3

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?

Mohammad Derakhshan
  • 1,262
  • 11
  • 33

2 Answers2

2

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
                    }
polis
  • 737
  • 7
  • 6
  • Many thanks for your response. However, this answer doesn't completely satisfy my need. If the user navigates from the sign-in screen to the login screen and then presses the back button because you are popping up until the route, the app closes, while the desired behavior is the user gets back to the sign-in screen. – Mohammad Derakhshan Aug 30 '22 at 14:05
1

Try this

I am imagining, login screen is your first screen.

While navigating from signup screen back to login screen

navController.navigateUp()

While navigating from login screen to signup screen

navController.navigate(AuthenticationNavGraph.SignInScreen.route)

It will simply navigate your graph one step up and will prevent creating a new entry of login screen in backstack.

primo
  • 1,340
  • 3
  • 12
  • 40
  • Many thanks for your response. Actually, there is a welcome screen at first with two buttons: login and sign up. I apologize for not making it clear. I added this note to my question, also. – Mohammad Derakhshan Aug 31 '22 at 06:41
  • Can you please tell me what is your expected behavoiur? – primo Aug 31 '22 at 08:27