collectEffects
Performs the effects of this router for as long as the screen is started.
viewModel.collectEffects { effect ->
when (effect) {
is NoteEffect.ShowSaved -> snackbarHostState.showSnackbar("Saved")
is NoteEffect.Close -> onClose()
}
}An effect is something only the UI can do, so it is performed only while there is a UI to do it:
Effects are collected while the lifecycle is at least started. An effect emitted while the screen is stopped, with the app in the background for example, waits in the router and is performed when the screen starts again.
Each effect runs in a coroutine of its own. One that suspends, such as showing a snackbar, does not hold up the next one.
onEffect may change between recompositions. The latest one is always the one called.
An effect that is being performed when the screen stops is cancelled and is not performed again, and one that had already left the router at that moment is still performed, so that none is lost. Call this once per router: a router gives each effect to one collector, so two collectors would each see only some of them.
Parameters
Performs one effect. It runs on the main thread.