0Pricing
Jetpack Compose Academy · Lesson

StateFlow vs SharedFlow

Choose the right hot stream for the UI.

StateFlow vs SharedFlow is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 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 Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Hot Streams for the UI

To stream live data into Compose you use a hot flow that stays active and emits whether or not anyone is currently collecting it.

Meet StateFlow

A StateFlow always holds exactly one current value. New collectors immediately receive that latest value, which is perfect for screen state. 📦

val count = MutableStateFlow(0)
count.value = 1

Read the Value Anytime

Because StateFlow caches one item, you can read its current value synchronously through the value property without collecting.

val ui: StateFlow<UiState> = _ui
val now = ui.value

StateFlow Skips Duplicates

A StateFlow only emits when the value actually changes. Setting it to the same value again sends nothing to collectors.

_count.value = 5
_count.value = 5 // no new emission

Meet SharedFlow

A SharedFlow broadcasts events to many collectors but holds no required current value. It is built for things that happen, not state that is.

val events = MutableSharedFlow<String>()

SharedFlow Replay Buffer

A SharedFlow can replay a few past emissions to late collectors. With replay zero, new collectors only see future events.

MutableSharedFlow<Int>(replay = 1)

SharedFlow Keeps Duplicates

Unlike StateFlow, a SharedFlow emits every value you send, even repeats. Two identical taps fire two separate events.

events.emit("tap")
events.emit("tap") // both delivered

State Versus Events

The rule of thumb: use StateFlow for what the screen looks like, and SharedFlow for one-time things like a navigation or a toast.

Expose Read-Only Streams

Keep a private mutable flow inside the ViewModel and expose a read-only one. The UI observes but cannot mutate it.

private val _ui = MutableStateFlow(UiState())
val ui: StateFlow<UiState> = _ui

StateFlow Needs a Default

A StateFlow must start with an initial value, so the UI always has something to show. A SharedFlow can start empty.

MutableStateFlow(UiState(loading = true))

Collect in Compose

You turn either flow into Compose state with a collector. For a StateFlow the screen has data immediately on first frame.

val state by viewModel.ui.collectAsStateWithLifecycle()

Quick Check

Match each flow to its purpose.

Recap: StateFlow vs SharedFlow

Nice! StateFlow caches one current value for screen state, while SharedFlow broadcasts repeatable events with an optional replay buffer. 🎯

Frequently asked questions

Is the “StateFlow vs SharedFlow” lesson free?

Yes — the full text of “StateFlow vs SharedFlow” is free to read here on the web, and the Jetpack Compose 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 Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “StateFlow vs SharedFlow”?

Choose the right hot stream for the UI. You practise Jetpack Compose 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 Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “StateFlow vs 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 Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose 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. StateFlow vs SharedFlow
  2. Mapping & Combining Flows
  3. stateIn & WhileSubscribed
  4. One-Off Events with Channels
← Back to Jetpack Compose Academy