Skip to content Skip to sidebar Skip to footer

How Can I Clear Every Fragments In Back Stack Using Navigation Component (for Example When HTTP 401 Triggeres) And Sent To Login Fragment

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 kn

Solution 1:

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


Post a Comment for "How Can I Clear Every Fragments In Back Stack Using Navigation Component (for Example When HTTP 401 Triggeres) And Sent To Login Fragment"