Collecting flows in a lifecycle-aware manner
summary
Explains how to stop and start Flow collection based on Android lifecycle by using repeatOnLifecycle, flowWithLifecycle, and collectAsStateWithLifecycle; repeatOnLifecycle spawns a coroutine at a given Lifecycle.State (commonly STARTED), cancels it when falling below that state and only returns when the lifecycle is destroyed; flowWithLifecycle is implemented with repeatOnLifecycle plus callbackFlow; collectAsStateWithLifecycle (alpha) wraps produceState for Compose so emissions update Compose state only while visible.
AI-written summary · about
chapters (9)
- 0:00Introduction
- 0:55Collecting in a lifecycleaware manner
- 2:17Collecting flows manually
- 2:42Lifecycle Libraries
- 3:20Repeat on Lifecycle
- 4:20Flow with Lifecycle
- 4:48Implementation details
- 5:11API
- 5:43Summary
related talks
36mLifecycle for Android Developers
30mA deep dive on the lifecycle-aware coroutines APIs
40mCompose Without Flow: Unidirectional State with Snapshots and Coroutines
38mBuilding StateFlows in Android with Jetpack Compose
36mGo with the Flow
23mMaster of Lifecycle
44mGoing with the Flow - Using Coroutines 1.3.x
45mCoroutines, Flow & Android- The Good Parts
