Home > Software engineering >  Can't pop starting destination using navigation and jetpack compose
Can't pop starting destination using navigation and jetpack compose

Time:10-23

I am using the latest versions of navigation and compose on Android, and i am getting a bug where i can't pop the starting destination of the navigation. The problem is, if i have 3 destinations (A,B,C) and go from A-> B -> C, i can't pop A from the backstack , but B is popped when i call popUpTo(B) inclusive = true, causing the back button to go back to A.

My code:

NavHost

setContent {

        val navController = rememberNavController()

        NavHost(
            navController = navController,
            startDestination = "route_to_a"
        ) {

            composable("route_to_a") {
                LoginScreen(navController)
            }

            composable("route_to_b") {
                RegisterScreen(navController)
            }

            composable("route_to_c") {
                HomeScreen(navController = navController)
            }
        }
    }

Navigations

- A to B

Button(onClick = { navController.navigate("route_to_b")}) {}

- B to C

Button(onClick = {
    navController.navigate("route_to_c") {
        popUpTo("route_to_b") {
            inclusive = true
        }
    }
}) {}

I want to create a flow where neither A and B are on the backstack after getting on C. But for some reason i can't remove A from the backstack...how can i do it?

CodePudding user response:

Replace,

popUpTo("route_to_b") {
    inclusive = true
}

with this,

popUpTo("route_to_a") {
    inclusive = true
}

From Docs,

// Pop everything up to and including the "home" destination off
// the back stack before navigating to the "friends" destination
navController.navigate("friends") {
    popUpTo("home") { inclusive = true }
}
  • Related