Erfannj En
10/25/2024, 7:01 PMStylianos Gakis
10/25/2024, 7:11 PMonClick = {
navController.navigate(topLevelRoute.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
popUpTo(navController.graph.findStartDestination().id) {
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
}
}
If you see no navigation at all happening, is there a chance you are navigating somewhere, but you restoreState = true
, so the restoration just restores the backstack to exactly what it is before you do anything, so you just see nothing going on as a result?Erfannj En
10/25/2024, 7:20 PMhttps://github.com/erfansn/ComposableScreens/blob/master/food/kristina_cookie/media/ComposableScreens_FoodGraph.png▾
navController.navigate(HomeRoute) {
popUpTo(HomeRoute) {
}
https://github.com/erfansn/ComposableScreens/blob/65ba11421fa71bce7eb38b3d5c8ca691[…]posablescreens/food/kristina_cookie/KristinaCookieNavigation.ktErfannj En
10/25/2024, 7:27 PMStylianos Gakis
10/25/2024, 7:36 PMHome -> Product -> Cart -> Product (again)
correct?
Because if that is the case, I am not sure I understand why you want to restore any state when going to product. It is a new entry to the backstack and should be on top of everything right?
If you then press the back button 3 times, do you expect to see this?
Product (you are here after the flow you described) -> Cart -> Product (the first product you saw) -> HomeIan Lake
10/25/2024, 7:49 PMnavController.navigate(HomeRoute) {
restoreState = true
popUpTo(HomeRoute) {
saveState = true
}
}
is equivalent to
// Pop everything up to HomeRoute
// Saving the state so that the next time
// you navigate to HomeRoute with restoreState = true
// that state is restored
navController.popBackStack(HomeRoute, inclusive = false, saveState = true)
// Navigate to HomeRoute, restoring the state that
// was saved on it
navController.navigate(HomeRoute) { restoreState = true)
Of course that isn't doing anything. You're restoring what you just saved, putting you exactly back where you were.Erfannj En
10/27/2024, 1:53 PMnavController.navigate(HomeRoute) {
restoreState = true
popUpTo(HomeRoute) {
inclusive = true
saveState = true
}
}
Erfannj En
10/27/2024, 2:09 PMStylianos Gakis
10/27/2024, 2:26 PMIan Lake
10/27/2024, 2:28 PMErfannj En
10/27/2024, 2:45 PMProduct
screen that I only want to run when navigating to it from the Home
screen and when returning to it. To achieve this, I used rememberSaveable
to keep a flag in the backstack and restore it to decide whether or not to run the animation.
The issue is that when I enable the save-and-restore mechanism, the arguments passed to the Product
screen are also included in the save-and-restore process, which causes issues.
Let me explain with an example to clarify. Suppose in the Cart
, products with IDs 2 and 3 already exist, and I want to add another product with ID 1.
Example flow:
1. Home -> Product(1)
– (Check the flag; since it’s “false,” run the animation, add the product to the cart, set the flag to “true,” and navigate to the Cart
while popping Product(1)
from the stack with state saving.)
2. Cart -> Product(2)
– (Navigate to Product(2)
with state restoring, check the flag, and since it’s “true,” don’t run the animation.)
3. Product(1)
I expected Product(2) but Product(1) delivered to me!!!!!!!! (Argument values in state save/restoration)Erfannj En
10/27/2024, 2:54 PMHome
screen. I’m using the following code for this, but when I tap the button, nothing happens:
navController.navigate(HomeRoute) {
restoreState = true
popUpTo(HomeRoute) {
inclusive = true
saveState = true
}
}
Erfannj En
10/27/2024, 2:57 PMStylianos Gakis
10/27/2024, 3:07 PMErfannj En
10/27/2024, 3:11 PMCart
screen and execute this code, it doesn’t navigate back to the Home
screen.Stylianos Gakis
10/27/2024, 3:24 PMIan Lake
10/27/2024, 5:46 PMrememberSaveable
state shared between Product(1) and Product(2) - that's not the right problem to be focused onIan Lake
10/27/2024, 5:49 PMnavController.previousBackStackEntry?.destination?.hasRoute<Home>()
as the conditional for starting your animation in Product might be enough without any of the rest of thisErfannj En
10/28/2024, 7:04 AMCart
screen to `Home`:
navController.navigate(HomeRoute) {
restoreState = true
popUpTo(HomeRoute) {
inclusive = true
saveState = true
}
}
Secondly, are the arguments provided for navigating to a specific route also included in the save process? If so, how can I prevent this?Ian Lake
10/28/2024, 2:16 PMIan Lake
10/28/2024, 2:17 PMErfannj En
10/30/2024, 6:56 PM