Hi!
I am looking for a method which can clear my backstack and make the navigated screen the only element in backstack
I have a method something like;-
Copy code
fun NavController.navigateAndClearBackstack(route: String) {
val navController = this
navController.navigate(route) {
popUpTo(navController.graph.findStartDestination().route.orEmpty()) {
inclusive = true
}
launchSingleTop = true
}
}
But it dosen’t work in all the cases , if my original startDestination is not the first element in backstack, this method won’t work.
Can anyone suggest me how to do if we do not know how my backstack looks like?
If it isn't then you already have bigger issues to fix, since you've already broken any handling of deep links, multiple back stacks, etc. that all assume you are following that correctly
Ian Lake
06/12/2024, 1:39 PM
But an easy fix here is to use
popUpTo(navController.graph.id)
- the graph itself is always on the back stack and is always the first entry
p
Prateek Kumar
06/12/2024, 1:46 PM
My startdestination is dashboard which needs login to function, so if a user logs out or not logged in, then i navigate them to a diff route(i still don't change my startDestination)
And i don't want users to go to the dashboard if they are not logged in, not sure how this can be fixed.
According to this video , my startDestination should never be login , and conditionally when login should be required , i should navigate them to login
That is what i am trying to do