How can I switch nested navigation graph in Jetpack Compose?

Viewed 575

For example, I have two graphs: LoginGraph and HomeGraph.
I want all the screens from the LoginGraph column to be shown to the user on the first visit to the application. Then, with successful authorization, I would save the boolean flag somewhere, for example, in shared preferences.

After the restart, I would read, if the flag is true, then show all the screens from the HomeGraph.

It is possible to explain a simple concept without full code (A plus would be with code fragments)

@Composable
fun RootNavigationHost(navController: NavHostController) {

    NavHost(navController = navController, startDestination = "Auth", route = "Root") {

        addAuthGraph(navController)
        addMainGraph(navController)
    }
}
private fun NavGraphBuilder.addAuthGraph(navController: NavHostController) {
    navigation(
        route = "Auth",
        startDestination = "Auth1"
    ) {
        composable("Auth1") {
            AuthScreen() {
                navController.navigate("Auth2")
            }
        }
        composable("Auth2") {
            PersonalDataScreen() {
                navController.navigate("Main")
            }
        }
    }
}
private fun NavGraphBuilder.addMainGraph(navController: NavHostController) {
    navigation(
        route = "Main",
        startDestination = "Home"
    ) {
        composable("Home") {
            HomeScreen() {
                navController.navigate("Other")
            }
        }
        composable("Other") {
            OtherScreen()
        }
    }
}
0 Answers
Related