Jetpack compose navigation popUpTo inclusive true not cleared composable screen from backstack
F

2

8

I am using Jetpack Compose ui version 1.3.0-beta08 and navigation compose version 2.4.0-alpha02 and working Bloom app with all new Architecture components. After login success I need to clear all previous compose login screens, but even added popUpTo("HomeScreen"){inclusive = true}, my login screen removed from backstack, but login welcome screen not removed from backstack, when I click back from Home screen. is it anything I am missing/wrong in compose navigation?

composable(Screen.LoginScreen.route) {
                LoginScreen {
                    Log.d("AppMainNavigation", "AppMainNavigation: ${navController.backQueue}")
                    navController.navigate(Screen.HomeBaseScreen.route){
                        popUpTo(Screen.LoginScreen.route){
                            inclusive = true
                        }
                        launchSingleTop = true
                    }
                }
            }

GitHub Link : https://github.com/rramprasad/BloomApp/blob/main/app/src/main/java/dev/ramprasad/bloom/MainActivity.kt

Flatfish answered 7/6, 2021 at 8:43 Comment(2)
Link doesn’t work.Phalan
Try popUpTo(0) Pop up will try to pop all non-matching destinations from the back stack until this destination is found. 0 id must not be in the backstack for this to workHonghonied
E
2

update your jetpack compose version to the latest release preview version and all other dependencies. current jetpack composes version: 1.0.0-rc02

and also which screen you want to popup just use this

my splash screen code:

 @Composable
 fun Navigate(navController: NavController) {
    produceState(initialValue = -1) {
        delay(1500)
        navController.popBackStack()
        val route = if (Firebase.auth.currentUser == null) AppRouter.Intro.route else AppRouter.Home.route
        navController.navigate(route)
    }
 }

so here you can use this below line to remove screen from backstack

navController.popBackStack()
Estremadura answered 27/7, 2021 at 1:14 Comment(3)
Unfortunately, this is doesn't help. My case is - I have a drawer with several items. And I want each item to clear the backstack. So when user clicks back on that screen - he finishes the app. As a last resort, I will listen to back click... But ideally, would like to solve this with navigation.Talia
@rocknow did you figure out?Vigilant
@rocknow did you find it?Footrope
K
0

Use WelcomeScreen's route as a parameter in popUpTo function. It clears all backstack's entries before WelcomeScreen, and if inclusive = true then WelcomeScreen too.

navController.navigate(Screen.HomeBaseScreen.route) {
    popUpTo(Screen.WelcomeScreen.route) { inclusive = true }
}

Also, if you use parameters in the route, you have to use them in popUpTo as well.

navController.navigate(Screen.HomeBaseScreen.route) {
    popUpTo(Screen.WelcomeScreen.route + "/{someId}") { inclusive = true }
} 
Kame answered 22/6, 2023 at 9:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.