Status Update
Comments
il...@google.com <il...@google.com> #2
Navigation only stores the state of entries in a back stack.
That means for the current back stack, the only way of clearing the state is to pop that destination off the back stack.
That means for a back stack you've specifically saved with saveState
, the only way of clearing that state is via clearBackStack
using the same route or ID you used with saveState
.
ja...@gmail.com <ja...@gmail.com> #3
you can save the State of an entry even if it's not in the backStack, for example, you want to save the state of the bottom navigation Items screen for later restoration, but it should not appear in the backstack because the user backstack should be clean.
I usually use the saveState for this kind of situation.
navController.navigate(destinations.route) {
launchSingleTop = true
restoreState = true
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
}
in this code, as far as I know, popUp to startDestination, and save all the screens' state for later restoration.
this is the saveState documentation:
Whether the back stack and the state of all destinations between the
* current destination and the [NavOptionsBuilder.popUpTo] ID should be saved for later
* restoration via [NavOptionsBuilder.restoreState] or the restoreState
attribute using
* the same [NavOptionsBuilder.popUpTo] ID (note: this matching ID is true whether
* [inclusive] is true or false).
means navigation can store the state of entries that are not in the backStack.
entries that are in the backtrack don't need any saveState, their state must be saved.
il...@google.com <il...@google.com> #4
If you've saved a back stack via saveState
, then clearBackStack
is how you clear that saved back stack and all of the state saved with it.
Please read the
Description
Component used: Navigation Version used: 2.7.1 Devices/Android versions reproduced on: all devices
we can save the state of the destination via saveState on the NavOptionsBuilder even if the destination is not present in the back stack.
there is a method in the NavController Class called clearBackStack that deletes saveState of the given destination, but it should be in the BackStack.
if a destination does not appear in the backStack, so how can I delete its savedState?
There should be an API to delete a savedState of a destination even if it is not in the backStack, something like
clearSavedState(route or Id)