collectAsStateWithLifecycle
Observe ViewModel state safely in Compose.
collectAsStateWithLifecycle is a free Jetpack Compose Academy lesson on CoddyKit — lesson 4 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.
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.
Frequently asked questions
Is the “collectAsStateWithLifecycle” lesson free?
Yes — the full text of “collectAsStateWithLifecycle” 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 “collectAsStateWithLifecycle”?
Observe ViewModel state safely in Compose. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “collectAsStateWithLifecycle” 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
- Why ViewModel Survives Rotation
- viewModel() in a Composable
- Modeling a UiState Data Class
- collectAsStateWithLifecycle