Issue
We have next attributes to remove fragment/fragments when opening another one.
app:popUpTo="..."
app:popUpToInclusive="true"
But in case when I need to clear all back stack not knowing which id was the first one (and start destination could have been already removed from stack) on 401 events for example.
How can I do it?
For example I may have 5 fragments in back stack and I don't know the id of the first one, and how can I remove them all and open a login fragment?
Solution
Updated answer:
We can use R.id.nav_graph
(id of our Navigation Graph) for setPopUpTo()
to clear backstack completely
val navOptions = NavOptions.Builder()
.setPopUpTo(R.id.nav_graph, true)
.build()
navController.navigate(R.id.loginFragment, null, navOptions)
Previous answer:
I implemented this solution, though it looks difficult
navController.graph = navController.graph.apply {
startDestination = R.id.loginFragment
}
val navOptions = NavOptions.Builder()
.setPopUpTo(R.id.loginFragment, true)
.build()
navController.navigate(R.id.loginFragment, null, navOptions)
But it works and clears all back stack and only one loginFragment will be in the stack
Answered By - user924
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.