collectAsStateWithLifecycle
Beobachten Sie ViewModel-State sicher in Compose.
collectAsStateWithLifecycle ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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.
Observing a Flow in Compose
Your ViewModel exposes state as a Flow, and your Composable needs to turn that stream into a value Compose can read and react to.
The Lifecycle-Aware Way
Use collectAsStateWithLifecycle() to collect a StateFlow into Compose state while respecting the screen’s lifecycle. 🔋
val ui by vm.state.collectAsStateWithLifecycle()Why Not collectAsState
Plain collectAsState() keeps collecting even when the app is in the background, wasting work. The lifecycle version pauses instead.
Stops in the Background
When your screen stops, collection stops too; when it returns to the foreground, collection resumes, saving battery and avoiding pointless updates.
Reading the Result
The call returns a State you read with by, giving you a normal value that triggers recomposition whenever the flow emits.
val ui by vm.state.collectAsStateWithLifecycle()
Text(ui.title)Add the Dependency
This helper lives in a runtime artifact. Add lifecycle-runtime-compose so the function is available in your Composables.
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.0")Choosing the Min State
By default collection is active while at least STARTED. You can lower the threshold, but STARTED is the safe, recommended choice.
Pairs With StateFlow
It shines with StateFlow, which always holds a current value, so your screen has something to show the instant it appears.
Branch on the State
Once collected, branch on the UiState to render loading, success, or error, all driven by the latest emission from your ViewModel.
if (ui.isLoading) Spinner() else Content(ui.items)Safer Than Manual Collection
It frees you from manually starting and cancelling collection. The helper ties the work to composition and lifecycle for you. ✨
The Modern Default
For new Compose screens, collectAsStateWithLifecycle() is the recommended way to observe ViewModel state safely and efficiently.
Quick Check
Check why the lifecycle-aware collector is preferred.
Recap
Observe ViewModel state with collectAsStateWithLifecycle(): it collects a StateFlow into Compose state, pauses in the background, and is the modern default.
Häufig gestellte Fragen
Ist die Lektion „collectAsStateWithLifecycle“ kostenlos?
Ja — der vollständige Text von „collectAsStateWithLifecycle“ 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 „collectAsStateWithLifecycle“?
Beobachten Sie ViewModel-State sicher in Compose. 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 4 von 4.
Wie lange dauert die Lektion „collectAsStateWithLifecycle“?
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
- Warum ein ViewModel Rotation übersteht
- viewModel() in einer Composable
- Eine UiState-Data-Class modellieren
- collectAsStateWithLifecycle