Christopher Mederos
10/17/2023, 3:06 AMuser/{userId}
. However, when I use restoreState=true
in the NavOptionsBuilder, I can only navigate to a given userId once, then all further navigations land at the same userId destination. Setting restoreState=false
fixes this issue, though I miss out on restoring the state of the viewmodel and composables from the previous time the destination was visited. How do I ensure the route argument is considered in the saveState used by navigation compose?NavHost(
navController = navController,
) {
composable(route = "users) {
UsersScreen(onSelectUser = {userId -> navController.navigateSingleTopTo("user/$userId")})
}
composable(
route = "user/{userId}",
arguments = listOf(navArgument("userId") { type = NavType.IntType })
) {backStackEntry ->
UserDetailScreen(
onBackNav = { navController.navigateSingleTopTo("users")}
)
}
}
And I'm using the nav function recommended by the android docs -
fun NavHostController.navigateSingleTopTo(route: String) =
this.navigate(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(
this@navigateSingleTopTo.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
}