StateFlow always holds the latest value and emits it immediately to new collectors. SharedFlow does not hold a value by default but can be configured to replay a set number of past values.
StateFlow always holds the latest value and emits it immediately to new collectors. It does not replay all past values, only the latest one.
Using lifecycleScope.launchWhenStarted ensures the SharedFlow is collected only when the Activity is visible, preventing missed events and avoiding leaks.
MutableStateFlow requires an initial value passed directly to its constructor.
val sharedFlow = MutableSharedFlow()
New subscribers do not receive any previously emitted values. Why?
MutableSharedFlow by default has replay set to 0, so it does not replay any past values to new subscribers.
