allan.conda
04/15/2022, 5:05 PMA -> B -> C
J -> K -> L
X -> Y -> Z
If app is in J, then it wants to go navigate to Y, how do I navigate to Y correctly?
I just call navController.navigate("Y"), but then the bottom bar now behaves weirdly, tapping the J bottom bar does nothing.
The bottom bar is showing because it’s logic for visibility is currentDestination.hierarchy.any { it.route in bottomDestinations } based
on official docs and samples.
Is there a better way?Ian Lake
04/15/2022, 5:10 PMallan.conda
04/15/2022, 5:22 PMYou are in full control over the source of truth for what tab is selected
Ian Lake
04/15/2022, 5:25 PMallan.conda
04/15/2022, 5:33 PMnavController.navigate("Y_navgraph") {
popUpTo("J_navgraph") { // specifically J, as A seems to work (probably because it's startDestination)
saveState = true
}
launchSingleTop = true
restoreState = true
}Ian Lake
04/15/2022, 5:35 PMallan.conda
04/15/2022, 5:45 PMnavController.navigate("Y_navgraph") {
popUpTo(navController.graph.findStartDestination().id) { // basically A
saveState = true
}
launchSingleTop = true
restoreState = true
}
Doing so I’m not sure the Up & Back button works correctly. I kind of expect it to go to X which is the parent of Y in the graph, or going back to J seems right as well. But it seems to go back to A (the start destination).
Is this as expected or am I still missing something? It’s a bit weird to me that Y gets navigated to as the startDestination of the X tab, I kind of expected the startDestination to be there…Ian Lake
04/15/2022, 5:49 PMrestoreState = true isn't going to do anything - there's nothing to restore. You've navigated to 'Y', so you get 'Y' as the only destination on that stack, exactly what you told NavController to doIan Lake
04/15/2022, 5:54 PM// clear any previously saved stack on X
navController.clearBackStack("X")
// navigate to "X", swapping tabs
navController.navigate("X") {
popUpTo(navController.graph.findStartDestination().id) {
// Make sure to save the state of "J"
saveState = true
}
}
// Now add "Y" on top of X
navController.navigate("Y")allan.conda
04/15/2022, 6:03 PMclearBackStack(route) which helped me understand saveSate better.
So saveState saved a back stack tagged to the current route (J) before doing the navigate(route) { popUpTo(…) } operation. Then restoreState must be trying to restore the back stack of navigate(X) IIUC. And in above snippet we don’t want to restore the backstack hence we don’t set restoreState=true.
I’m curious if clearBackstack here is necessary given we are not restoring `X`’s back stack anyway.Ian Lake
04/15/2022, 6:05 PMViewModel or saved state associated with those previous destinations is properly cleaned upallan.conda
04/15/2022, 6:06 PMX without restoring the state? assuming I don’t saveState from X again.Ian Lake
04/15/2022, 6:09 PMIan Lake
04/15/2022, 6:09 PMallan.conda
04/15/2022, 6:11 PMnavController.navigate("X") {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
navController.navigate("Y")
And this one works nicely for me. Pressing Back from Y goes back to X, then X back to A.allan.conda
04/15/2022, 6:13 PM