I need to clarify a naive expectation I had: 1. I ...
# compose-android
t
I need to clarify a naive expectation I had: 1. I have a LaunchedEffect(Unit) { .. } in a high level screen 2. Using navigation, I navigate away from that screen, and then popStack back to it 3. When I reenter the original screen, the LaunchedEffect runs again. I though the whole point of a LauncedEffect(Unit) was that it would only happen the first time it composed. Great reminder that you shouldn't code with any expectations about when your composition might rerun. But I am still curious why it did rerun. Does the navigation stuff flush the current compositions when it navigates to a new one?
m
When you navigate away from a screen, it means that that screen will be disposed. You can think of it as a simple when condition:
Copy code
// Nav
when (screen) {
    Screen.First ->
        FirstScreen()

    Screen.Second ->
        SecondScreen()
}

// FirstScreen
LaunchedEffect(Unit) {
    // Will run when the composable enter navigation for the first time
}
The pop is going to change the current screen state back to Screen.First which is going to dispose
SecondScreen
and compose
FirstScreen
for the first time.
t
I guess I had assumed that it was more like:
Copy code
if (firstLink) {
  ScreenFor(firstLink)
}
if (secondLink) {
  ScreenFor(secondLink)
}
if (thirdLink) {
  ScreenFor(thirdLink)
}
basically, that the the compositions for the "covered" links would remain to drop back to, at least in a cache maybe
Basically, that it is/was like a series of Dialogs
z
Screens on the backstack have their composition disposed, because it’s expensive and should only hold view state which largely is irrelevant for things not actually being drawn. For exceptions to those cases there’s rememberSaveable, and that data is preserved for screens on the backstack.
It’s the same with Views - most navigation frameworks will get rid of views for screens in the backstack, but will save critical data for them via saveInstanceState
t
So that's why my lists are still scrolled to the point they were at last when I pop back to them?
👍🏻 1
z
Exactly. If you’re trying to keep a task, running across multiple screens, it probably belongs in your presenter layer or above, not the view layer.