Skip to content
Go back

SharedFlow vs Channel for Events

Published:

Events are triggers or short-lived state, like calling navigation or temporarily showing a popup.

SharedFlow

SharedFlow drops events when there are no active listeners (think onStopped). Unless you set replay = 1. In this case new subscribers also get old events replayed (think configuration change).

Channel

Channel buffers events when there are no listeners. Channel returns the event to first (only!) active listener and removes the event from buffer.

Official recommendation

Treat events as normal state instead. Meaning write it to UI state and notify ViewModel after it has been handled. This would reset that state. source

Result

Channel - usually perfect for a single listener.
Flow - may drop events (can be desirable), or have duplicate events.
UI state - robust alternative, testable, verbose.



Next Post
Git Worktrees