What is a Jetpack ViewModel and the problem it solves?

This tests your understanding of lifecycle-aware state preservation. A good answer explains that ViewModels survive configuration changes (like rotation) to hold UI data, separating it from the transient UI controller.
What's really being asked
This question tests your understanding of the Android Activity/Fragment lifecycle, specifically the destructive and re-creative nature of configuration changes like screen rotation. The interviewer wants to confirm you know why state is lost and how ViewModel is the architecturally correct way to manage UI-related data across these events, separating it from the transient UI controller.
The full answer
First, explain the core problem: configuration changes cause Android to destroy and recreate the Activity/Fragment, discarding any in-memory UI state. Second, introduce ViewModel as a class designed to store and manage UI-related data, whose key feature is that it survives these configuration changes. When the Activity is recreated, it receives the same ViewModel instance. Third, clarify that a ViewModel is scoped to a lifecycle owner (Activity/Fragment) and lives until that owner is permanently destroyed, not just reconfigured. Finally, contrast its purpose with onSaveInstanceState: ViewModel is for complex, in-memory data (e.g., network results), while onSaveInstanceState is for small amounts of simple state needed to survive system-initiated process death.
The mistakes people make
A major red flag is confusing ViewModel's purpose with onSaveInstanceState. Saying a ViewModel survives process death on its own is incorrect; that requires the Saved State module. Another weak answer is simply saying "it saves data" without explaining the lifecycle context or the problem of configuration changes. Finally, describing it as a general data store is wrong; it's specifically for UI-related data and should not hold references to Views or Activities, which would cause memory leaks.
What usually comes next
Expect questions like, "How would you share a ViewModel between two Fragments?" (Answer: Scope it to the parent Activity). Or, "When is a ViewModel's onCleared() method called?" (Answer: When the associated Activity is finished or the Fragment is permanently detached). A tougher one is, "So how do you handle process death with ViewModel?" (Answer: You use the Saved State module for ViewModel, which integrates with the onSaveInstanceState bundle).
A concrete example
Imagine a screen fetching 500 user profiles from a network. Without a ViewModel, rotating the screen would destroy the Activity and its list of 500 profiles, forcing a new, expensive network call. With a ViewModel, the list is held in the ViewModel instance. The new Activity instance simply reconnects to the existing ViewModel and immediately displays the data, saving network, battery, and time.
Interview question
A user rotates their device while on a screen displaying data fetched from a network. What is the main benefit of using a ViewModel in this situation?
- a.It automatically saves the data to the onSaveInstanceState bundle for efficient restoration.
- b.It cancels the old Activity's network request and starts a fresh one for the new orientation.
- c.It ensures the data is permanently saved to a local database for offline access.
- d.It retains the fetched data in memory, so the new Activity instance can use it immediately without re-fetching.Correct
Why? this is the answer
A ViewModel's primary purpose is to survive configuration changes, like rotation, allowing it to hold UI data in memory. This prevents costly re-fetching, which is the problem described. The onSaveInstanceState bundle is meant for small amounts of data, not complex network results.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
- #android
- #jetpack
- #viewmodel
- #lifecycle
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