how to pop the backstack I’m not currently on / al...
# compose
s
how to pop the backstack I’m not currently on / all multiple backstacks at once (e.g. on logout)? [thread]
1
I have multiple backstacks - ABCDE (bottom nav) • I start with “A” (as already logged in user) • I go to C, then a level deeper to some details screen C2 • I go to E, which has logout button • logout does popBackstack to “A” • “A” conditionally navigates me to Login screen, because I’m logged out • Logging in navigates to “A” • C2 is still restored on the other stack I need to pop all stacks to default state while logging out - so next logged in user has the same experience as if cold app run happened If needed, my navigation from login back to “A” looks like this:
Copy code
val homeRoute = AppScreen.Home.createRoute(root)
navController.navigate(route = homeRoute) {
   popUpTo(homeRoute) {
      inclusive = true
   }
   restoreState = false
}
a
Change popUpTo(C) { inclusive = true }
s
change = add this line? at which point?
I’m not able to modify stack C while being on stack A
Copy code
Ignoring popBackStack to destination 706351978 as it was not found on the current back stack
the issue is, there are multiple backstacks and I can be levels deep in all of them at the same time. I can be at A2, B2, C2 etc, then I have to reset them to their roots after logout. This way freshly logged-in user doesn’t have any nesting which previous user navigated into
a
navController.backQueue.retainAll(arrayListOf(navController.getBackStackEntry("Startroute")))
i
Never, ever, ever, ever access the private
backQueue
It sounds like you want to be calling
navController.clearBackStack()
, which is what clears a saved back stack
a
You must have route or destinationid. How to clear all except Start destination
s
thanks a lot @Ian Lake. so I solved this by looping over backstack roots to call clearBacktack on each of them, then navigated to first root graph - which makes it exactly how fresh start would look like
👍 2