Michal Klimczak
04/08/2022, 1:23 PMfun NavController.navToBottomNavItem(route: String, startFromStackRoot: Boolean = false) = navigate(route) {
//how to use [startFromStackRoot]?
popUpTo(DashboardScreenDestination.route) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
The scenario in our app is, there's a botom menu and there's a Dashboard stack (app starts on that) and a Shop stack. The code above makes sure that when user navigates between different bottom bar items, he isn't starting from the root of the stack, but ends up where he left. So far so good.
But there's one scenario, where I want to go from Dashboard directly to the root of the Shop stack, without restoring it's state. E.g. using the startFromStackRoot
param.backStackStates
and we can't just e.g. find and clear a backstack by some id. Just invoking restoreState
conditionally will work (it will start from root) but the old Shop stack state will still remain saved and it will be restored on another occasion while what we want is for the fresh one to be savedIan Lake
04/08/2022, 7:42 PMMichal Klimczak
04/08/2022, 10:25 PMIan Lake
04/08/2022, 11:14 PMwe can't just e.g. find and clear a backstack by some idBut that's exactly what
clearBackStack()
does: https://developer.android.com/reference/kotlin/androidx/navigation/NavController#clearBackStack(kotlin.String)clearBackStack()
followed by navigate()
seems like it would do exactly what you want?Michal Klimczak
04/09/2022, 5:02 AMIan Lake
04/09/2022, 6:08 AMroute
when you declared your destination. That exact string, `{id}`s and allMichal Klimczak
04/09/2022, 8:30 AM