Lifecycle, LiveData & repeatOnLifecycle
ARCHITECTURE › Components
Lifecycle-aware data observation: LiveData, LifecycleOwner, repeatOnLifecycle, and Flow tradeoffs.
Interviewers use this topic to check whether you can keep UI in sync with data without leaking memory or crashing on stopped screens. Expect questions on LiveData's active/inactive model, lifecycle states and observers, and the modern way to collect Flows safely with repeatOnLifecycle or collectAsStateWithLifecycle. Strong answers explain WHY launchWhenStarted is deprecated and when to pick Flow over LiveData.
What this covers
- How LiveData ties observers to a LifecycleOwner and only delivers to active (STARTED/RESUMED) observers
- setValue vs postValue, MutableLiveData encapsulation, and map/switchMap/MediatorLiveData
- Lifecycle.State vs Lifecycle.Event and using DefaultLifecycleObserver over the deprecated annotation approach
- Why repeatOnLifecycle(STARTED) is the correct way to collect Flows in the View system
- Why launchWhenStarted/launchWhenX are deprecated (they suspend instead of cancel the producer)
- Flow vs LiveData tradeoffs and the data-layer (Flow) to ViewModel (asLiveData/StateFlow) pattern
Study this topic
- Lifecycle, LiveData & repeatOnLifecycle explained: the guided lesson
- 14 practice quiz questions
- 9 revision flashcards
- Lifecycle, LiveData & repeatOnLifecycle interview questions and answers