androidandroid-jetpack-composeandroid-jetpack-navigationjetpack-compose-navigation

Compose-Navigation: Remove previous composable from stack before navigating


I'm using compose-navigation(alpha09) to handle the navigation between composables

I want to remove the Splash screen when moving to the next destination (I don't want the back pressed to get back to Splash)

Following attempts did not work as expected:

navHostController.navigate(Route.login.id) {
    navHostController.graph.clear()
}
navHostController.navigate(Route.login.id)
navHostController.graph.clear()
val currentDest = navHostController.currentDestination
navHostController.navigate(Route.login.id)
if (currentDest != null) {
   navHostController.graph.remove(currentDest)
}

So how can I remove the Splash screen and then move to next?


Solution

  • In Jetpack Compose 1.0.0 to navigate and remove the previous Composable from the back stack You can use:

    navController.navigate(Screens.Login.name) {
        popUpTo(Screens.Splash.name) {
            inclusive = true
        }
    }
    

    The above code will navigate from the Splash screen to Login and will pop everything up, including the Splash screen.

    Navigate to a composable - docs