Reputation: 754
I've been searching on this topic for a while now, without any success. Since the last update of Navigation Component 2.5.0 in combination with BottomNavigationView
the nav controller retain the stack state for each tab of BottomNavView
.
Now, I am asking if there is any way to exclude a graph
or some fragments
from this rule ?
More in details:
activity
and the rest are fragments
BottomNavigationView
with 4 tabsgraph
(no confusions) - seems clean, especially in a bigger applicationhelp
and settings
)Why do I want to exclude some fragments (help
and settings
) from this rule to retain their state? Well, because navigating to one of them and then changing the BottomNavigationView
tabs back and forth, I can end up having these fragments over all of my 4 tabs (or those 4 main graphs)
What I have tried?
settings
and faq
into the BottomNavigationView
but without being visible, and just link the global action to that tab (manually setting the current item of the BottomNavView
) - failed, because I've ended up with 6 items in the BNV
and it will cause a crash, as more than 5 items is a design concern.BNV
and the other to take care of the global actions, of course I ended up with a bit of a mess.Maybe I am asking too much from Android
, but I would like to be able to keep this default behaviour added in 2.5.0
for Navigation
but at the same time write some exceptions from it. I wouldn't mind manually handling the navigations for each BNV
tabs, but my issue is with the global actions
.
Any suggestions would be highly appreciated.
Upvotes: 1
Views: 478
Reputation: 328
Now, I am asking if there is any way to exclude a graph or some fragments from this rule ?
You can exclude the entirety of the NavigationBarView
from the state preservation by using an overload of setupWithNavController
with saveState
set to false. However, you cannot exclude certain fragments/routes as you like. It's either all or none, as far as NavigationUI
is concerned.
As I see it, you have three viable options:
NavigationUI
source to implement exclusionsNavHostFragment
s, as in your 2nd attempthelp
or settings
fragments are active (hide the BNV with NavController#addOnDestinationChangedListener
?)I'd say the second option is by far the ideal one here, as it is also what actually makes sense hierarchically from the way you've defined your app behavior: The global actions need not be coupled to your BNV tabs.
This answer nicely demonstrates how you would obtain the NavController
references. It is actually not too bad to work with once you've gotten a grasp of what NavController
needs to be doing what. In fact, it's arguably the least hacky design you could go for.
Upvotes: 1