I followed guide from android.developers and implemented a navigation component
into my app.
I stumbled across a problem when I need some screens to be with or without a toolbar/bottom navbar.
Android developers example's layout
<androidx.appcompat.widget.Toolbar
.../>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
.../>
forces me to hide/show toolbar/bottomNavBar in OnDestinationChanged
callback in MainActivity:
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination) {
R.id.topLevelDestination-> {
toolbar.visibility = View.GONE
bottomNav.visibility = View.VISIBLE
}
R.id.lowLevelDestination -> {
toolbar.visibility = View.VISIBLE
bottomNav.visibility = View.GONE
}
And, of course, if I do it like this I have my layout resize before I see new fragment. I mean I see how bottom nav disappears on fragment A, and I see fragment's B parts in the place where bottomNavBar was when fragment A is still on the screen, and after that fragment, B appears.
How to solve it? Do I need nested nav graphs?
update: Added a gif of problem
Video description: it's a cutted part of my screen. On video you can see system UI, bottom nav bar and main fragment with a button. When i click button nav graph navigates me to a destination without bottom navigation bar. So, I do bottomNavBar.hide() when OnDestinationChanged. As you can see, bottomNavBar is disappeared BEFORE I actually navigate and you can see part of my destination fragment visible after bottomnavBar gone. That's the problem.
addOnDestinationChangedListener
you can hide/show toolbar from each fragment'sonStart()
. Have you considered this approach? – SickleMutableLiveData<Boolean>
to set visibility of Toolbar, and set it in appropriate lifecycle method of the fragments, onCreateView, onViewCreated, onStart or onResume – QuiescentNavHostFragment
of ViewPager inside a fragment of a bottom bar to set navigation for current graph or set visibility of viewpager(this one has some issues). You can check out if you wish here – Quiescent