Android Jetpack Navigation proper back stack with BottomNavigationView
This is currently not supported in the new Navigation Architecture. I was also pretty bummed by this, as it is a very basic feature in today's apps, and a lot of the apps are now using the bottom navigation. There is a running thread, if you wanna keep an eye on it. They are saying they will come up with a long term solution for this, but for the shorter run, they are gonna give a sample on how to tackle this. https://issuetracker.google.com/issues/80029773#comment25
The major reason is you only use one NavHostFragment
to hold the whole back stack of the app.
So the solution is each tab should hold its own back stack.
- In your main layout, wrap each tab fragment with a
FrameLayout
. - Each tab fragment is a
NavHostFragment
and contains its own navigation graph in order to make each tab fragment having its own back stack. - Add a
BottomNavigationView.OnNavigationItemSelectedListener
toBottomNavigtionView
to handle the visibility of eachFrameLayout
.
If you don't want to keep all the fragments in memory, you can use app:popUpTo
and app:popUpToInclusive="true"
to pop out the ones you don't want to keep.