If I want to switch screens when some `MutableStat...
# compose
j
If I want to switch screens when some
MutableStateFlow
variable becomes a certain value, what is the best approach to do that in a Composable? I know that Composables should mostly be side-effect effect free, and this action definitely seems like a side effect
h
One easy way: Consume the flow as state and use it in a when to show different screens.
j
This is how I do it currently:
Copy code
val result = stateFlow.collectAsStateWithLifecycle()

when(result) {
    is Result.SwitchScreen1 -> {
        // Push Screen1
    }
    is Result.SwitchScreen2 -> {
        // Push Screen2
    }
    ...
}
This logic is safe to include inside a composable?
h
If you show the screen it is safe. If you push some state to some kind of navigation object which changes your screens you either need a side effect or do it without the composable, for example during updating your flow.
j
I am using a navigation object, but I am calling
navigator.push(Screen1())
without giving it any state. Not sure if that counts
f
if you use a state flow, when you return to the original screen it will push again to the 2nd screen, you need a means to consume these events
the AAC docs suggest having navigation in the UI; if you have something else triggering navigation, you should consider modelling those events as state, and then having the UI notify that it is triggering navigation based on that state so that the state can be cleared
j
Got it, thanks!