<https://developer.android.com/develop/ui/compose/...
# compose
c
https://developer.android.com/develop/ui/compose/navigation#bottom-nav
Copy code
navController.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?
Untitled.kt
ScreenRecording2024-10-29at7.05.49pm-ezgif.com-optimize.gif
I've seen some recommendations to not start the nav graph with a conditional screen like login when working with fragments, but it's not clear to me if this is still the case with navigation-compose https://developer.android.com/guide/navigation/use-graph/conditional
s
but it's not clear to me if this is still the case with navigation-compose
Yes, it definitely is still the case with navigation-compose
👍 1
navController.graph.findStartDestination().id
When 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
.
If in your tab navigation code you change it with
Copy code
navController.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.
👍 1
c
ok gotcha - thanks for the good info IRL, most of my login happens in a seperate activity anyhow. There is just some tidying up I do inside this activity since I'm using FirebaseAuth prebuilt ui (regret lol). Perhaps I'll just put the entire navhost in an if-else block and show a progress indicator until the app is fully warmed up
s
Perhaps I'll just put the entire navhost in an if-else block and show a progress indicator until the app is fully warmed up
What 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]
c
ok interesting, that sounds a bit more graceful. Haven't used installSpashScreen before so I'll have to give it a try
Thanks for the code links too 😎
😎 1
s
No worries, good luck with it!