Android Academy · Lesson

StateFlow and SharedFlow

Expose reactive state from your app.

Lesson 4 of 413 steps

StateFlow and SharedFlow is a free Android Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

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.
  • stateIn converts cold flows to StateFlow.
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “StateFlow and SharedFlow” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “StateFlow and SharedFlow”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. Coroutine Basics and Scopes
  2. viewModelScope and Dispatchers
  3. Kotlin Flow Basics
  4. StateFlow and SharedFlow
← Back to Android Academy