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
hfhbd
05/27/2023, 7:16 AM
One easy way: Consume the flow as state and use it in a when to show different screens.
j
Justin Xu
05/27/2023, 7:26 AM
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
hfhbd
05/27/2023, 7:36 AM
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
Justin Xu
05/27/2023, 8:33 AM
I am using a navigation object, but I am calling
navigator.push(Screen1())
without giving it any state. Not sure if that counts
f
Francesc
05/27/2023, 3:45 PM
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
Francesc
05/27/2023, 3:47 PM
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