Process death lifecycle
Process Death Lifecycle Deep Dive¶
Overview¶
Android can kill app processes under memory pressure. Robust apps restore state correctly when process is recreated.
What Survives and What Doesn't¶
Lost on Process Death¶
- In-memory objects
- ViewModel instances
- Ongoing in-process jobs
Can Be Restored¶
savedInstanceStateUI state- Persisted data from Room/files/preferences
Recovery Pattern¶
- Save lightweight UI state in
onSaveInstanceState. - Keep source of truth in persistent storage.
- Rehydrate screen state from repository on recreation.
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("scroll", recyclerView.computeVerticalScrollOffset())
}
Architecture Guidance¶
- UI state: Bundle +
SavedStateHandle. - Durable state: Room/DataStore/network cache.
- Avoid relying only on ViewModel for critical data.
Interview Traps¶
- "ViewModel survives process death" -> false.
- Assuming
onDestroy()always runs before death -> false.
Key Takeaways¶
- Design for process death from day one.
- Persist essentials and restore deterministically.