Skip to content
tezvyn:

How does a ViewModel survive configuration changes and what is its scope?

Source: developer.android.comMediumHow cards are made

How does a ViewModel survive configuration changes and what is its scope?
Summary

knowledge of ViewModelStore and lifecycle scope.

Key points

the framework retains ViewModels in a store scoped to an Activity or Fragment across config changes, clearing them only when the owner finishes for good.

What's really being asked

This question probes whether you understand the boundary between framework-managed UI state and instance state in the Jetpack lifecycle architecture. Specifically, the interviewer wants to know if you can explain why a ViewModel outlives an Activity during rotation but still respects final cleanup, and whether you understand the difference between a ViewModelStore and a LifecycleOwner. They also care if you know why holding data in a ViewModel is preferable to holding it directly in an Activity, since the Activity is recreated on every configuration change while the ViewModel is not.

The full answer

First, scope definition: a ViewModel is scoped to a ViewModelStoreOwner, which is typically an Activity or a Fragment. Second, survival mechanism: when a configuration change occurs, the Activity instance is destroyed, but its associated ViewModelStore is retained by the framework and reattached to the new Activity instance. Third, lifecycle endpoint: the ViewModelStore clears all ViewModels only when the owner is finishing permanently, which happens when onDestroy is called and isChangingConfigurations returns false, or when the process is killed. Fourth, practical implication: because the ViewModel object itself is reused, you can hold UI-related data such as network results or user selections without serializing them into a Bundle. Fifth, threading awareness: since the ViewModel can survive configuration changes, long-running operations started inside it are not interrupted by rotation.

The mistakes people make

A major red flag is claiming that onSaveInstanceState preserves the ViewModel; that API only handles small, parcelable data in a Bundle, not live objects. Another mistake is saying the Activity itself survives rotation or that a static map holds the ViewModel; neither is true. Some candidates also confuse ViewModel scope with process scope, implying the data lives forever even if the app is swiped away, which is incorrect. A subtler error is forgetting that a ViewModel should never hold a direct reference to an Activity or View, since the old Activity is leaked during recreation.

What usually comes next

The interviewer may ask how ViewModel works with SavedStateHandle for process death, or why you should not pass a ViewModel between Activities. They might also ask about the difference between Activity-scoped and Fragment-scoped ViewModels, or how Hilt injects them into different owners. Another common follow-up is asking how to share a ViewModel between a parent Activity and its Fragments using the Activity as the ViewModelStoreOwner.

A concrete example

Imagine an Activity that fetches a list of photos. The user rotates the phone. The Activity is torn down and recreated, but the ViewModel containing the list and the active coroutine job remains in the ViewModelStore. The new Activity requests the same ViewModel via the ViewModelProvider, receives the existing instance, and immediately shows the cached list without refetching. When the user presses back, onDestroy signals a true finish, the ViewModelStore calls clear on the ViewModel, and the coroutine is cancelled. If the system kills the process in the background, the ViewModel is gone, which is why you might pair it with SavedStateHandle for critical parcelable state.

Interview question

When an Activity is destroyed during a screen rotation, why does its associated ViewModel remain available to the new Activity instance?

  • a.The Activity instance is not actually destroyed; only its views are recreated.
  • b.The framework retains the ViewModelStore and reattaches it to the new Activity instance.Correct
  • c.A static map maintained by the framework caches the ViewModel across all Activities.
  • d.The ViewModel is serialized into onSaveInstanceState and restored automatically.
Why?

The framework retains the ViewModelStore across configuration changes and reattaches it to the new Activity instance, so the same ViewModel object is reused. The most tempting distractor, onSaveInstanceState, only persists small parcelable data in a Bundle and cannot restore live objects like a ViewModel.

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