🐦 Kotlin Intermediate

What is SharedFlow in Kotlin?

Answer

SharedFlow is a hot flow that broadcasts emissions to all active collectors simultaneously. Unlike StateFlow, it does not hold a current value and new collectors do not receive past emissions by default (configurable via replay parameter). It is ideal for one-time events like navigation commands, error messages, or analytics events that should not be re-delivered when the UI resubscribes. Configure the extraBufferCapacity and onBufferOverflow to handle backpressure when collectors are slow. In a ViewModel, expose it as SharedFlow and emit via the MutableSharedFlow.