Christopher Mederos
10/29/2024, 6:19 AMnavController.navigate(topLevelRoute.route) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
I'm following the example from the navigation docs for integrating bottom nav. However, when I implement the same pattern, I'm not getting the expected launchSingleTop & restoreState behaviour.
Am I correct in reading this snippet? I'm under the impression that tapping on a bottom nav should return to the previous state of that corresponding navigation graph? IE - the destination & state of the last visited composable on that tab should be restored, instead of starting fresh again at the start destination of the nav graph?Christopher Mederos
10/29/2024, 8:03 AMChristopher Mederos
10/29/2024, 8:08 AMChristopher Mederos
10/29/2024, 8:56 AMStylianos Gakis
10/29/2024, 9:22 AMbut it's not clear to me if this is still the case with navigation-composeYes, it definitely is still the case with navigation-compose
Stylianos Gakis
10/29/2024, 9:25 AMnavController.graph.findStartDestination().idWhen you do this after you've already popped the Login destination from your backstack, I would assume it actually finds nothing to pop up to, and it simply does not. So I assume it does not pop anything, so it does not save anything, so then it simply navigates to the other tab normally, not being able to restore anything. If the backstack does not then also infinitely grow as you go from tab to tab it may be because you're also doing
launchSingleTop
.Stylianos Gakis
10/29/2024, 9:27 AMnavController.navigate(bottomTabRoute.route) {
popUpTo<TabA> {
saveState = true
}
launchSingleTop = true
restoreState = true
}
This may just work, since you'd be popping up to the right tab.
But I'd still try to make it work with TabA
being the real start destination anyway.Christopher Mederos
10/29/2024, 9:33 AMStylianos Gakis
10/29/2024, 9:36 AMPerhaps I'll just put the entire navhost in an if-else block and show a progress indicator until the app is fully warmed upWhat we do is keep the splash screen still on the screen until we know if we must go to the login graph or not. And then we observe the auth status, and when we know about it we remove the splash screen, and bserving the auth state automatically navigates people to the login graph [1] [2]
Christopher Mederos
10/29/2024, 9:47 AMChristopher Mederos
10/29/2024, 9:47 AMStylianos Gakis
10/29/2024, 9:48 AM