When ViewModels Go to Die: Saving ViewModel State
summary
Explains how Jetpack SavedStateHandle lets a ViewModel persist transient UI state across system-initiated process death rather than using persistent storage; demo builds a tiny app, adds dependency lifecycle-viewmodel-savedstate:2.2.0, replaces manual Bundle plumbing with constructor-injected SavedStateHandle, initializes via SavedStateViewModelFactory (or a custom AbstractSavedStateViewModelFactory for extra args), and proves survival with an adb shell am kill test; notes limits: only small/parcelable data and risk of “transaction too large,” and it doesn’t restore after user force-close.
AI-written summary · about
chapters (30)
- 0:00Introduction and welcome
- 0:53About the speaker
- 1:34What is state in Android
- 2:16State vs activity lifecycle
- 3:18ViewModels and state management
- 4:01Process death and its impact
- 5:03Resource constraints and process death
- 6:04ViewModels and process death loss
- 7:07Real-world implications of process death
- 8:12Live coding demo setup
- 9:38Simulating process death with ADB
- 10:20Limitations of saved instance state
- 11:23How saved instance state works
- 12:47Problems with saved instance state
- 13:30Introducing saved state handle
- 14:32Benefits of saved state handle
- 15:14Making app process death proof
- 15:56Adding saved state handle dependency
- 16:17Updating ViewModel with saved state
- 17:43ViewModel factory setup
- 18:28Initializing ViewModel with factory
- 19:34Testing process death survival
- 21:01Handling constructor arguments
- 22:04Creating custom ViewModel factory
- 23:30Updating fragment with custom factory
- 24:13How saved state handle works behind scenes
- 26:44ViewModel and view responsibilities
- 27:26Limitations of saved state handle
- 28:08Benefits for developers and users
- 28:30Q&A session begins
