Almeric
10/30/2023, 7:52 AMNavHost(
modifier = Modifier.padding(it),
navController = appState.navController,
startDestination = "homeGraph"
) {
navigation(
startDestination = "home",
route = "homeGraph"
) {
composable("home") {
Home("A") {
appState.navController.navigate("home2", null)
}
}
composable("home2") {
Greeting("B")
}
}
composable("settings") {
Greeting("C")
}
}
fun navigateToTopLevelDestination(topLevelDestination: TopLevelDestination) {
val topLevelNavOptions = navOptions {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
when (topLevelDestination) {
TopLevelDestination.HOME -> navController.navigate("homeGraph", topLevelNavOptions)
TopLevelDestination.SETTINGS -> navController.navigate("settings", topLevelNavOptions)
}
}
And here’s a recording of the issue:Stylianos Gakis
10/30/2023, 9:07 AMAlmeric
10/30/2023, 11:03 AMStylianos Gakis
10/30/2023, 11:20 AMascii
10/30/2023, 11:23 AMstartDestination = "home2"
you'll see that pressing back from C will always open B.Stylianos Gakis
10/30/2023, 11:30 AMIf you setThen you are breaking the entire graph, why would they want to change the entire start destination for this?you’ll see that pressing back from C will always open B.startDestination = "home2"
other than mutating startDestination via remember + mutableStateOfThat to me sounds like a bad idea. If you do that, then if you are in B and you want to do the back gesture, then what, would you exit the app directly from “home2"? If that’s the start destination you’d also want it to be the beginning of the backstack, so as you try to go back you’ll see the back gesture exiting the app and then you’d actually exit the app, no?
ascii
10/30/2023, 11:52 AMwhy would they want to change the entire start destination for this?Just an example to help them understand
Ian Lake
10/30/2023, 2:00 PMpopUpTo(navController.graph.findSta rtDestination().id)
so the only thing left on the back stack (e.g., what happens when you do system back) is the start destination of the graph, so what you're seeing is exactly what you said you wanted to see.dorche
10/30/2023, 11:19 PMIan Lake
10/30/2023, 11:31 PMpopUpTo(navController.graph.findStartDestination().id)
, that is the start destination of your entire graph. It is actually assumed that it is part of your bottom nav, not the opposite, but that's more of a convention than requirementAlmeric
10/31/2023, 7:47 AMsaveState = true
in the PopupToBuilder
this would also restore the state when popping ‘back’ to this destination.