Observe As Events
Observes a Flow and executes the onEvent callback for each emitted value in a lifecycle-aware manner.
Uses LaunchedEffect and repeatOnLifecycle to ensure flow collection occurs only when the composable's lifecycle is at least in minActiveState (defaults to Lifecycle.State.STARTED).
Employs rememberUpdatedState so that onEvent always invokes the most recent lambda instance across recompositions without restarting the underlying flow collection.
Parameters
The Flow to observe.
An optional key that, if changed, will restart flow collection.
Another optional key that, if changed, will restart flow collection.
The minimum Lifecycle.State required to collect events. Defaults to Lifecycle.State.STARTED.
Callback invoked for each value emitted by flow.
Type Parameters
The type of data emitted by the Flow.
Extension function variant of ObserveAsEvents for convenient fluent syntax.
Example:
viewModel.uiEvent.ObserveAsEvents { event ->
when (event) { ... }
}