0Pricing
Jetpack Compose Academy · Lektion

StateFlow vs. SharedFlow

Wählen Sie den passenden Hot Stream für die UI.

StateFlow vs. SharedFlow ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🎯

Häufig gestellte Fragen

Ist die Lektion „StateFlow vs. SharedFlow“ kostenlos?

Ja — der vollständige Text von „StateFlow vs. SharedFlow“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „StateFlow vs. SharedFlow“?

Wählen Sie den passenden Hot Stream für die UI. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?

Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „StateFlow vs. SharedFlow“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?

Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. StateFlow vs. SharedFlow
  2. Flows abbilden und kombinieren
  3. stateIn und WhileSubscribed
  4. Einmalige Ereignisse mit Channels
← Zurück zu Jetpack Compose Academy