Android & Jetpack Compose (50 Qs)•Part 3: ART Internals, Memory Leak Profiling & Production Hardening (25 - 35)
Coroutine Lifecycle in Android: `repeatOnLifecycle` vs `launchWhenStarted`.
senior LevelLifecyclePerformanceMemory Management
In Simple Words: Collecting flows naively on Android can cause catastrophic background leaks:
launchWhenStarted(Deprecated & Dangerous): Pauses coroutine execution when the app enters the background, but keeps the active upstream Flow subscription open! The server or location manager continues streaming data, burning battery in the background.repeatOnLifecycle(Lifecycle.State.STARTED): Completely cancels and restarts the collection block when the Activity stops and restarts, ensuring zero background battery drain or resource leaks.