I have a question about navigation, Let's say I ha...
# compose
n
I have a question about navigation, Let's say I have two screens in my bottom navigation bar,
Home
and
Explore
And the navigation code in the
BottomBar
looks like this:
Copy code
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 screen
first video is the case where Explore is not popped up or
NavGraph
(Includes home and explore) is popped up, and the second is the behavior I expect (it only pops up Explore alone)
here is the navigation structure of my app
i
It sounds like you should be calling
navController.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