Nthily
02/07/2024, 5:44 PMHome
and Explore
And the navigation code in the BottomBar
looks like this:
navController.navigate(screen.direction) { // navigate to other bottom screen
popUpTo(destination.route) { // clean current screen stack
saveState = true
inclusive = true
}
restoreState = true
}
If I'm just navigating back and forth in BottomBar
, like Home -> Explore -> Home
, the ViewModel instance in Home and Explore is the same.
BUT, if I perform a redirecting to Home after Home -> Explore -> Home
(which can be understood as I need to perform this behavior in the case of changing accounts), the VM instance of Explore will still not be destroyed Because Explore isn't popping up the back stack, but in this case, is there a good way to pop up all the back stacks?
Suppose there is another case that is Home -> Explore -> Settings -> Home
then -> Home
If Explore
and Settings
are not popped up , the VM instances for Explore and Settings will still be the same when re-navigate to the Home screenNthily
02/07/2024, 6:01 PMNavGraph
(Includes home and explore) is popped up, and the second is the behavior I expect (it only pops up Explore alone)Nthily
02/07/2024, 6:05 PMSean Proctor
02/07/2024, 7:06 PMIan Lake
02/07/2024, 7:10 PMnavController.clearBackStack()
, passing it the route of each of your bottom nav items, when you log out if you want those back stacks and their associated ViewModels to be destroyed