Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent multiple creation of a screen in jetpack compose navigation

I have a welcome screen containing two buttons:Login and Sign Up. Hence I have a screen for each one: login screen and sign up screen.

Both of them are in the same NavGraphBuilder. Users can navigate from sign up screen to login screen and vice versa.

At the moment, when the user is on the login screen and clicks on the signUp screen, the screen is added to backQueue even though it already exists.

I try to prevent new destination recreation by applying singleTop like this:

navController.navigate(AuthenticationNavGraph.SignInScreen.route) {
   this.launchSingleTop = true
}

But no progress. So how can I prevent new screen recreation if the screen already exists on backQueue?

like image 251
Mohammad Derakhshan Avatar asked Oct 18 '25 15:10

Mohammad Derakhshan


1 Answers

Please try restoreState = true :

     navController.navigate(item.route) {
                        // Pop up to the start destination of the graph to
                        // avoid building up a large stack of destinations
                        // on the back stack as users select items
                        navController.graph.startDestinationRoute?.let { route ->
                            popUpTo(route) {
                                saveState = true
                            }
                        }
                        // Avoid multiple copies of the same destination when
                        // reselecting the same item
                        launchSingleTop = true
                        // Restore state when reselecting a previously selected item
                        restoreState = true
                    }
like image 194
polis Avatar answered Oct 21 '25 03:10

polis