How does a ViewModel survive configuration changes?

Tests understanding of lifecycle-aware components. ViewModels are retained by a ViewModelStore owned by the Activity/Fragment, which survives configuration changes. The ViewModel is cleared only when its scope is permanently finished.
What's really being asked
This question probes your knowledge of Android's component lifecycles and modern state management patterns. The interviewer wants to see that you understand why ViewModel exists and how it solves the specific problem of retaining UI state during configuration changes, where the Activity instance is destroyed but the user's task is not finished. It separates candidates who only know the basic Activity lifecycle from those who understand modern architecture.
The full answer
A strong answer walks through the mechanism in four steps. First, ViewModels are managed by a ViewModelProvider. Second, this provider retrieves the ViewModel from a ViewModelStore, which is owned by a ViewModelStoreOwner (like an Activity or Fragment). Third, this ViewModelStore is the key component; it is not destroyed during configuration changes. When the Activity is recreated, the new Activity instance connects to the same ViewModelStore and receives the existing ViewModel instance. Finally, clarify the scope: the ViewModel lives until its associated scope is permanently destroyed (e.g., the Activity finishes via finish() or the user navigates away), at which point its onCleared() method is called.
The mistakes people make
A major red flag is conflating ViewModel with onSaveInstanceState. ViewModels do NOT survive process death; they are in-memory objects. Stating they do is a critical error. Another mistake is being vague, saying it "just survives" without mentioning the ViewModelStore and the scoping mechanism. A junior answer might also incorrectly tie the ViewModel's lifecycle to the Application context, which is too broad a scope and generally an anti-pattern for UI-related data.
What usually comes next
"How would you handle state that does need to survive process death?" (Answer: SavedStateHandle injected into the ViewModel). "What's the difference between a ViewModel scoped to an Activity versus a Fragment?" (Answer: Scope and data sharing; a Fragment ViewModel is destroyed with the Fragment, while multiple fragments can share an Activity-scoped ViewModel). "When exactly is onCleared() called?" (Answer: When the ViewModelStoreOwner is permanently destroyed, e.g., Activity.finish()).
A concrete example
Imagine a user is filling out a complex registration form. They rotate their phone. The Activity instance is destroyed and a new one is created. Without a ViewModel, all typed data would be lost. With a ViewModel, the form data (e.g., MutableStateFlow<String>) is held in the ViewModel instance. The new Activity instance gets the same ViewModel from the ViewModelStore and re-populates its UI from the ViewModel's state. This works for large, complex data (like a 2MB user-selected bitmap) that cannot be put in onSaveInstanceState's Bundle, which is limited to about 1MB on recent devices.
Interview question
When an Activity is recreated after a screen rotation, what is the primary mechanism that allows it to retrieve the exact same ViewModel instance?
- a.The new Activity instance connects to a retained ViewModelStore that holds the original ViewModel.Correct
- b.The ViewModel's data is automatically saved to the onSaveInstanceState Bundle and restored.
- c.The ViewModel is tied to the Application context, so it persists as long as the app is running.
- d.The Android system serializes the ViewModel object to disk and deserializes it for the new Activity.
Why? this is the answer
The ViewModel is retained in a ViewModelStore object, which is not destroyed during configuration changes. The new Activity instance connects to this same store to retrieve the existing ViewModel. `onSaveInstanceState` is for smaller data and also handles process death, which ViewModels do not.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles