android/codelab-android-compose

[NavigationCodelab] Arguments retrieved by navBackStackEntry

jlgconde opened this issue · 3 comments

Hi, the accountType retrieved by the navBackStackEntry in the Rally app of the Navigation Codelab only works the first time. Afterwards, no matter which account I click, it goes to the single account I clicked the first time. The accountType value doesn't change :( Any help would be welcomed.

Hi, I've commented the restoreState = true line that is part of the solution and the app works properly now. I would like to understand what's happening and why, if this is just a mistake in the codelab and if so what's the proposed solution. Thanks in advance for any support!

Hi, I've commented the restoreState = true line that is part of the solution and the app works properly now. I would like to understand what's happening and why, if this is just a mistake in the codelab and if so what's the proposed solution. Thanks in advance for any support!

The bug happens because NavHost isn't able to treat the routes "accounts/Checking", "accounts/Home Savings", etc. as different routes, and it is restoring the state of the route ignoring the argument.

The best solution for this is set inclusive parameter to true inside the popUpTo call. This way NavHost will see the argument of your route too.

popUpTo(this@navigateSingleTopTo.graph.findStartDestination().id) {
               saveState = true
               inclusive = true
}