https://kotlinlang.org logo
#compose
Title
# compose
a

Abhishek Dewan

03/02/2022, 6:39 AM
I have a navigation back stack in my app that can be in one of two states: 1. Splash -> Home 2. Splash -> Landing -> Sign_In -> Home The following is the navigation code that navigates to the HomeScreen
Copy code
override fun navigateToHome() {
    navController.navigate(RootScreen.Home.route) {
        val popUpRoute = navController.currentBackStackEntry?.destination?.route ?: ""
        popUpTo(popUpRoute) {
            inclusive = true
        }
    }
}
I would like to make sure that I can clear the back stack completely and then navigate to home. The way I understand navigation, calling popUpTo(currentScreenOnBackStack) should clear all the backstack before we navigate to Home and should account for both scenarios above. I’m sure I’m misunderstanding something obvious. Would appreciate if someone could point it out for me.
Issue currently is that the system works for 1. But for 2 it doesn’t clear the back stack. It does clean the Sign_In page from the back stack but not Landing and Splash
c

Csaba Szugyiczki

03/02/2022, 7:54 AM
popUpTo means to which destination you want to clear the current back stack. If your back stack looks like this:
Splash -> Landing -> Sign_In
the code you posted navigates to the Home route, and when you say
navController.currentBackStackEntry?.destination?.route
that is basically your current route which is the
Sign_In
route currently. So your popUpTo will pop your current back stack up until the
Sign_In
route including the
Sign_In
route itself. But your Splash and Landing route will not be affected. What you want to do in both case 1 and 2 is passing the
Splash
route to the popUpTo as a parameter, so in both cases you will basically pop every screen off the back stack and then have only your
Home
route in it after the navigation
🙌 1
🙏 1
a

Abhishek Dewan

03/02/2022, 3:17 PM
Ok this is definitely informative, 🙏 . Yes it does work with passing in splash screen’s route always. I think I was thinking of the poping of the backstack in correctly
👍 1
2 Views