Hi! I am looking for a method which can clear my b...
# compose
p
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?
y
use pop until
i
Your original start destination should always be the first element of the back stack, as per the Principles of Navigation: https://developer.android.com/guide/navigation/principles#fixed_start_destination
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
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
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.

https://www.youtube.com/watch?v=09qjn706ITA&t=2s

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