Is there a way using compose navigation to navigat...
# compose
j
Is there a way using compose navigation to navigate back/up and return to a new instance of a screen? My app design calls for returning to a previous screen but it should be a new instance (all state cleared/ new ViewModel created) that will re-execute some asynchronous operation once the screen is displayed. I considered a regular
NavController.navigate(ScreenA)
with a
popup(ScreenA) { inclusive = true }
but I would like to preserve the default back animation. Using the following operations don’t return the result I’m expected:
Copy code
navController.popBackStack<ScreenA>(inclusive = false, saveState = false)
navController.popBackStack<ScreenA>(inclusive = true, saveState = false)
j
I think you should be able to do something like
Copy code
navController.navigate<ScreenA> {
  popUpTo<ScreenA>(inclusive = true)
}
j
This effectively puts a new destination on the back stack (while removing the old instance) which doesn't have the "back/popping" animation.
Ideally, I would like for the returning screen to slide in from the left edge of the screen.
j
You should be able to override the transition animation for that to match "going back" Though that might confuse users, they'll see they're "going back" but the screen they're back to has a different state
j
I tried playing with the transitions but couldn't find a good way to handle the enter transition for the "returning screen" when it is initially navigated too. I only want a custom transition when navigating back to it from
ScreenB
. As for your previous point, it's not a big deal that the state isn't retained. The screen is performing an asynchronous scan that just indicates when it's done or an error state.
j
Perhaps it makes more sense to change how that screen retains state then? Such that when you return to it, it does the scan again
j
I'm not sure of a good way to do that...I wanted to use a view model so the scanning operation could be unit tested and scans wouldn't restart across device rotations.. Any suggestions? I had considered
DisposableEffect
but I wasn't sure if it was a misuse of that API. I planned on researching tomorrow.
j
At work we've used the backstackentry's savedstate to store a "should refresh" flag before, but I'm not super happy with that solution tbh Assuming you're on Android, you can turn off activity restarts on rotation and have a LaunchedEffect call the scan function too
j
Compose Multiplatform app
j
Does iOS reload screens on rotate?
j
I have not tested if iOS reloads screens. That's something I will look into tomorrow.
Either way I would still like to use a VM to hold my scanning logic.
j
For sure, it should still live in the VM I'm saying triggering that scan could be done from a LaunchedEffect, and Android apps can (probably should with 100% Compose) be configured to not restart activities on rotate
j
Once I get the code in front of me I will verify if the activity is recreated on rotation
🙏 1