Skip to content
tezvyn:

What problem does Jetpack ViewModel solve during configuration changes?

Source: developer.android.comEasyHow cards are made

What problem does Jetpack ViewModel solve during configuration changes?

This tests your understanding of state loss during configuration changes. A good answer explains that Activities are destroyed on rotation, and ViewModel survives this event, preserving UI state.

What's really being asked

This question tests your understanding of a fundamental problem in Android development: state management across configuration changes. The interviewer wants to see that you don't just know what a ViewModel is, but why it exists. It's a proxy for understanding the Activity/Fragment lifecycle, memory management, and the separation of concerns between UI controllers and data models.

The full answer

A strong answer has three parts. First, define the problem: configuration changes like screen rotation cause the system to destroy and recreate the current Activity or Fragment. This means any non-persistent data held in memory by that component is lost. Second, explain the solution: a ViewModel is a lifecycle-aware component designed to survive these specific configuration changes. When the Activity is recreated, it receives the same ViewModel instance that existed before the change, preserving its data. Third, clarify its scope: the ViewModel is finally cleared and its onCleared() method is called only when the associated Activity is finished or the Fragment is permanently detached, not just during recreation.

The mistakes people make

A major red flag is confusing the role of ViewModel with onSaveInstanceState. ViewModel is for surviving configuration changes and holding complex UI data in memory. onSaveInstanceState is for surviving system-initiated process death and only handles small amounts of serializable data. Another mistake is saying a ViewModel never gets destroyed; it's tied to the UI controller's lifecycle scope. Finally, suggesting that a ViewModel can hold a direct reference to an Activity or View is a serious red flag, as this creates a memory leak.

What usually comes next

"When would you use onSaveInstanceState instead of a ViewModel?" (Answer: For process death, and for small, simple, serializable data). "How does a ViewModel actually survive? Where is it stored?" (Answer: It's held by a ViewModelStore which is managed by the Activity/Fragment and retained by the system across configuration changes). "What is the onCleared() method used for?" (Answer: Releasing resources, like cancelling coroutines or cleaning up data stream subscriptions, when the ViewModel is about to be destroyed).

A concrete example

Imagine a user has typed "San Francisco" into a search box in a weather app but hasn't hit search yet. If they rotate their phone, the Activity is destroyed and recreated. Without a ViewModel, the text "San Francisco" would disappear. By storing this string in a MutableStateFlow or LiveData within a ViewModel, the new Activity instance can observe this data and immediately repopulate the search box, providing a seamless user experience.

Interview question

What primary problem does a Jetpack ViewModel solve during a device configuration change like screen rotation?

  • a.It stores small, serializable data to recover from system-initiated process death.
  • b.It prevents the Activity or Fragment from being destroyed during a configuration change.
  • c.It holds direct references to Views to quickly restore their state after recreation.
  • d.It preserves UI-related data that would otherwise be lost when the associated UI controller is recreated.Correct
Why?

ViewModel is designed to hold UI-related data in memory across configuration changes, ensuring that data like user input isn't lost when the Activity or Fragment is destroyed and recreated. Option A describes the purpose of onSaveInstanceState, which is for process death, not ViewModel's primary role during configuration changes.

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles