ViewModel: Surviving Android Configuration Changes

A ViewModel acts like a durable backpack for your UI's data, surviving the destruction and recreation of an Activity during a screen rotation. It holds UI state, like user input or fetched data, preventing data loss.
Why it exists
Android destroys and recreates UI controllers like Activities on configuration changes, such as screen rotation or language switching. This default behavior wipes out any state held in memory, like user input or fetched data, creating a jarring experience and forcing unnecessary work like new network calls.
The mental model
A ViewModel is a data container designed to outlive the UI controller it's associated with. It's like a durable backpack for your UI's data. When an Activity is destroyed during rotation, it's temporarily detached from its ViewModel. When the new Activity instance is created, it reconnects to the exact same ViewModel instance, finding its data intact.
How it works
When you request a ViewModel for a specific scope (like an Activity), the Android framework acts as a custodian. It checks if a ViewModel instance for that scope already exists. If so, it returns the existing one. If not, it creates a new one and retains it. This retained object is only cleared when the scope is permanently destroyed, not during temporary configuration changes.
When to use it
Use a ViewModel to store and manage all data the UI displays. This is the correct place to hold user input from forms, lists of data fetched from a server, and the general state of your screen. It acts as the single source of truth for the UI, exposing its data through observable streams like StateFlow or LiveData.
When not to use it
Never store references to Views, Composables, or a Context in a ViewModel. This is a classic memory leak. The ViewModel outlives the UI, so holding a reference to a destroyed Activity prevents it from being garbage collected. Also, a ViewModel by itself does not survive process death; it is not a substitute for persistent storage or the SavedStateHandle.
One canonical example
A user types a search query into an EditText. They rotate their phone. Without a ViewModel, the Activity is recreated and the EditText is now empty. With a ViewModel, the query is stored in a StateFlow. The new Activity instance connects to the existing ViewModel, observes the StateFlow, and immediately restores the query to the EditText. The user's input is seamlessly preserved across the configuration change.
Interview question
How does an Android ViewModel ensure that UI data persists across a screen rotation?
- a.It maintains a direct reference to the previous Activity instance to retrieve its data.
- b.It triggers a re-fetch of all necessary data from its source when the new Activity is created.
- c.It automatically saves all UI state to a local database before the Activity is destroyed.
- d.The Android framework retains the ViewModel instance, allowing the new Activity to reconnect to it.Correct
Why? this is the answer
The card explains that the Android framework acts as a custodian, retaining the ViewModel instance so the new Activity can reconnect to the existing one. Storing a direct reference to an Activity in a ViewModel (option A) is explicitly warned against as it causes memory leaks.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
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