简体   繁体   中英

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 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?

I implemented this solution, though it looks diffucult

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 clears all back stack and only one loginFragment will be in the stack

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM