StateFlow and SharedFlow
Expose reactive state from your app.
StateFlow and SharedFlow is a free Android Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Hot Flows
Unlike cold flows, StateFlow and SharedFlow are hot: they exist independently of collectors and can be shared among many.
StateFlow Is a State Holder
A StateFlow always holds one current value. New collectors immediately receive that latest value. It is perfect for UI state.
private val _count = MutableStateFlow(0)
val count: StateFlow<Int> = _count.asStateFlow()Updating StateFlow
Set .value directly or use update for atomic read-modify-write.
_count.value++
// or, atomically:
_count.update { it + 1 }StateFlow Conflates and Deduplicates
StateFlow only keeps the latest value (conflation) and skips emitting if the new value equals the old one. This avoids redundant UI updates.
SharedFlow for Events
A SharedFlow has no required current value and can replay a configurable number of past emissions. It suits one-off events like navigation or showing a snackbar.
private val _events = MutableSharedFlow<String>()
val events: SharedFlow<String> = _events.asSharedFlow()Emitting Events
Emit into a SharedFlow from a coroutine.
fun onSave() {
viewModelScope.launch {
_events.emit("Saved!")
}
}Why Not StateFlow for Events
StateFlow keeps the last value, so a re-subscribing collector (after rotation) would re-receive the old event and, say, navigate twice. SharedFlow avoids replaying by default.
Configuring SharedFlow
Tune replay and extraBufferCapacity to control buffering and back-pressure behavior.
MutableSharedFlow<String>(
replay = 0,
extraBufferCapacity = 1
)Converting a Cold Flow to StateFlow
Use stateIn to turn a cold upstream into a shared StateFlow scoped to the ViewModel.
val uiState = repo.stream()
.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5000),
UiState()
)Choosing Between Them
- StateFlow → current screen state (always has a value).
- SharedFlow → transient events (fire once).
Collecting Both
Collect StateFlow with collectAsStateWithLifecycle in Compose; collect SharedFlow events inside a LaunchedEffect to handle them once.
LaunchedEffect(Unit) {
vm.events.collect { msg -> showSnackbar(msg) }
}Quick Check
Test your understanding of StateFlow and SharedFlow.
Recap
You learned:
- StateFlow is a hot, conflated state holder with a current value.
- SharedFlow is a hot event stream with configurable replay.
- Use StateFlow for state, SharedFlow for one-off events.
stateInconverts cold flows to StateFlow.
Frequently asked questions
Is the “StateFlow and SharedFlow” lesson free?
Yes — the full text of “StateFlow and SharedFlow” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “StateFlow and SharedFlow”?
Expose reactive state from your app. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “StateFlow and SharedFlow” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Coroutine Basics and Scopes
- viewModelScope and Dispatchers
- Kotlin Flow Basics
- StateFlow and SharedFlow