stateIn und WhileSubscribed
Cachen Sie Flows und beenden Sie Arbeit, wenn niemand zuhört.
stateIn und WhileSubscribed ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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.
From Cold Flow to Hot State
UI state should be hot and cached, but your transformed flows are cold. The stateIn operator converts a cold flow into a StateFlow.
What stateIn Does
stateIn launches the upstream flow in a scope, shares one collection across all collectors, and caches the latest value for new ones.
val ui = source.stateIn(
viewModelScope, started, initial)It Needs a Scope
The first argument is a scope, usually viewModelScope, which ties the running flow to the ViewModel lifecycle.
flow.stateIn(viewModelScope, ...)It Needs an Initial Value
Because a StateFlow always holds a value, stateIn requires an initial one to show before the first real emission arrives.
flow.stateIn(scope, started,
initialValue = UiState.Loading)The Sharing Strategy
The middle argument is the SharingStarted policy. It decides exactly when the upstream flow starts and stops running.
Eagerly and Lazily
SharingStarted.Eagerly starts the flow immediately and never stops it, while Lazily starts on the first collector and keeps going.
started = SharingStarted.EagerlyMeet WhileSubscribed
WhileSubscribed only runs the upstream flow while there is at least one active collector, then stops it when the last one leaves.
started = SharingStarted.WhileSubscribed()The Five Second Timeout
Passing a small timeout to WhileSubscribed keeps the flow alive briefly after the last collector, surviving quick rotations.
SharingStarted.WhileSubscribed(5000)Why the Timeout Helps
During a screen rotation the old collector leaves and a new one arrives moments later. The timeout avoids a needless restart in between.
Stops Wasted Work
With WhileSubscribed a database or network flow pauses when the screen is off, then resumes when the user returns. Battery wins. 🔋
A Full stateIn Recipe
Put it together: transform your data, then call stateIn with the scope, WhileSubscribed, and an initial loading state.
val ui = repo.flow().map { UiState(it) }
.stateIn(viewModelScope,
SharingStarted.WhileSubscribed(5000),
UiState.Loading)Quick Check
Reason about subscriber-driven lifecycles.
Recap: stateIn & WhileSubscribed
Well done! stateIn turns a cold flow into a cached StateFlow, and WhileSubscribed runs it only while observed so you stop wasting work. 🎯
Häufig gestellte Fragen
Ist die Lektion „stateIn und WhileSubscribed“ kostenlos?
Ja — der vollständige Text von „stateIn und WhileSubscribed“ 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 „stateIn und WhileSubscribed“?
Cachen Sie Flows und beenden Sie Arbeit, wenn niemand zuhört. 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 3 von 4.
Wie lange dauert die Lektion „stateIn und WhileSubscribed“?
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
- StateFlow vs. SharedFlow
- Flows abbilden und kombinieren
- stateIn und WhileSubscribed
- Einmalige Ereignisse mit Channels