Lifecycle for Android Developers
summary
Claims that collecting Kotlin flows naively wastes CPU and memory because StateFlow and SharedFlow keep emitting without collectors, demonstrates that launching collects in lifecycleScope or with launchWhenStarted continues producing while the UI is backgrounded, and shows concrete fixes: use repeatOnLifecycle / flowWithLifecycle and the newer collectAsStateWithLifecycle from lifecycle-ktx to automatically cancel and relaunch collectors tied to lifecycle states. LiveData contrasts and Compose collection caveats are shown with working examples and exact failure modes.
AI-written summary · about
chapters (8)
- 0:00Lifecycle concepts
- 3:51Lifecycle owners and registry
- 6:56LiveData vs Flows
- 12:14Flows in UI problems
- 20:03Lifecycle-aware flow collection
- 27:10Repeat on lifecycle deep dive
- 33:11Compose lifecycle handling
- 35:19Summary and best practices
related talks
07mCollecting flows in a lifecycle-aware manner
23mMaster of Lifecycle
24mUsing Kotlin Flow in MVVM
30mA deep dive on the lifecycle-aware coroutines APIs
36mGo with the Flow
45mFalsehoods Android Developers Believe About Lifecycles
40mCompose Without Flow: Unidirectional State with Snapshots and Coroutines
19mKotlin Flows and Channels for Android
