how to redirect user to login page after sign out with jetpack compose navigation

Viewed 929

I'm using jetpack compose in my project. My app contains multiple screens and nested navigation. So I have this in my MainActivity:

   val startDestination = if (setting.isUserLoggedIn)
            HomeNavGraph.HomeRoute.route
        else
            AuthenticationNavGraph.AuthenticationRoute.route

        setContent {
            val navController = rememberNavController()
            DoneTheme {
                NavHost(
                    navController = navController,
                    startDestination = startDestination
                ) {
                    authentication(navController = navController)
                    home()
                }
            }
        }

This code decides that, based on whether the user has already logged in, navigate the user to the authentication screen or home screen. The home navigation is like this:

fun NavGraphBuilder.home() {
    navigation(
        startDestination = HomeNavGraph.HomeScreen.route,
        route = HomeNavGraph.HomeRoute.route
    ) {
        composable(route = HomeNavGraph.HomeScreen.route)
        {
            HomeRouteScreen()
        }
    }
}

The HomeRouteScreen() per se, has bottom navigation and its navHost. Now I want to navigate the user from the profile screen in HomeRouteScreen() to the Authentication screen that its composable is defined in the MainActivity nav graph.

my problem is:

If I call navcontroller.navigate(AuthenticationNavGraph.AuthenticationScreen.route), I get the error that the destination is not explicitly in the current nav graph. On the other hand, if I use the navController which exists in Main Activity, in HomeRouteScreen I get the error that ViewModelStore should be set before setGraph call. So How should I handle this issue?

0 Answers
Related