Context
We're migrating to use the nav component in my company, and its going ok so far.
We have a bottom navigation view with 5 tabs, and using the NavigationUI
to set it up.
We have "Home" as the start destination tab for our nav graph.
Using version 2.4.2
of the navigation-*
libraries.
Problem
Each tab now has its own backstack, and its state is retained, however, when:
- Having the "Home" tab opened, then opening FragmentA pushed, (Now backstack of that Tab is "Home" -> "FragmentA").
- Then switching to another tab, let's call it TabX.
- Then clicking on the hardware back button.
Expected
As pressing back would dismiss the current tab's stack, we get back to the "Home" tab with its previous state intact? (FragmentA pushed on top of it).
What happens
We go back to the "Home" tab with only the Home fragment, FragmentA is not showing. And the weird part is, when clicking again (reselecting) the Home tab, it now shows the previously saved state (FragmentA on top of Home).
As this is not the best UX ever, what should be done in this case? is any of those behaviours expected?
Thanks in advance!
You can check your fragments are also the same as the navigation id.
for navigation popup, you can use
findNavController().popBackStack()
orAlso, make sure you override the
onBackPressed()
method from the host activity code as:override fun onBackPressed() { finish() super.onBackPressed() }