Android Under the Hood: How ViewModel, State, and Restoration Work
Series: Android Under the Hood: How ViewModel, State, and Restoration Work (5 Parts)
- Part 1 of 5
ViewModel in Activity Under the Hood: How It Survives Recreation
What actually happens to ViewModel when Activity is destroyed and recreated? We break down the entire chain: ViewModelStore, NonConfigurationInstances, ActivityThread, and ActivityClientRecord. We show in detail where and how state is stored, and how Android magically restores everything during configuration changes.
- Part 2 of 5
ViewModel in Fragment Under the Hood: From ViewModelStore to Retain Fragments
How fragments manage their ViewModelStore, what FragmentManagerViewModel is, why child fragments are needed, and why Retain fragments are considered deprecated. A complete breakdown of architectural connections and call chains that allow fragments to preserve state during recreation.
- Part 3 of 5
ViewModel Under the Hood: How it Works in Compose and View
What happens under the hood when we call viewModel() in Compose or View? How does ViewModelStoreOwner lookup work, what does LocalViewModelStoreOwner do, how does Compose connect to the View hierarchy, and how are DI delegates like hiltViewModel() and koinViewModel() structured? A deep technical look at the scoping and state mechanism.
- Part 4 of 5
SavedStateHandle and Bundle Under the Hood: How Android Saves State
A comprehensive article about how Android saves and restores state: from onSaveInstanceState and Bundle to modern architecture with SavedStateHandle and SavedStateRegistry. How everything is connected to the lifecycle of Activity, Fragment, and ViewModel, what roles ActivityThread, Instrumentation, and ActivityClientRecord play. A complete low-level data flow that demystifies all the magic moments.
- Part 5 of 5
Decompose and Essenty: Under the Hood of State Saving without ViewModel
In this article, we dive deep inside Decompose and Essenty: how they save state without using ViewModel and onSaveInstanceState, what happens at the StateKeeper and InstanceKeeper level, how all this relates to Android SavedStateRegistry and serialization through kotlinx.serialization. A detailed, step-by-step analysis of the entire chain — from components to low-level details.