Search code examples
androidandroid-jetpack-composeandroid-jetpack-navigation

How to save and restore navigation state in Jetpack Compose?


I want to navigate to another screen, but i want to save current state, and then restore it. I tried to do as described in the bottom navigation documentation:

navController.navigate(Screen.CameraScreen.route) {
        popUpTo(navController.graph.id) {
            saveState = true
        }
    restoreState = true
}

But it doesn't work.


Solution

  • Try this:

    navController.navigate(Screen.CameraScreen.route) {
            navController.graph.startDestinationRoute?.let { route ->
                popUpTo(route) {
                    saveState = true
                }
            }
            launchSingleTop = true
            restoreState = true
        }