Viewmodel
25 bites tagged Viewmodel — interview questions with model answers, and 60-second explainers.
How does a ViewModel survive configuration changes and what is its scope?
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. knowledge of ViewModelStore and lifecycle scope.
Unit test a ViewModel exposing StateFlow in a local JVM test
Tests coroutine hygiene and flow collection strategy. A strong answer covers injecting a TestDispatcher, using runTest, collecting emissions in a background scope, and asserting states.
Why mock a Repository dependency in a ViewModel unit test?
This tests your grasp of test isolation. A great answer says mocking eliminates real database or network dependencies so you can verify ViewModel state changes in isolation. A red flag is insisting on using the real Repository inside the unit test.
What is the difference between viewModelScope and lifecycleScope?
This tests scope ownership across configuration changes. viewModelScope survives rotation because it lives in ViewModel, while lifecycleScope dies with UI; use former for logic and latter for UI tasks. Never launch data loads in lifecycleScope.
How do you inject UserRepository into a ViewModel with Hilt?
Tests Hilt constructor injection for classes you own. Annotate UserRepository's constructor with @Inject so Hilt auto-provides it to consumers like a @HiltViewModel. Red flag: using a @Provides module for a class you control.
How would you architect state-driven UI with ViewModel and Compose?
Tests unidirectional data flow and state hoisting. Model screen state as an immutable data class, expose StateFlow from ViewModel, and collect it in Compose so events flow up and state down. Red flag: exposing mutable state or putting logic in Composables.
How would you architect two source Flows from ViewModel to data layer?
This tests merging parallel async streams into one reactive state. A strong answer uses combine on two cold repository Flows, exposes one UI state Flow from the ViewModel, and maps failures to error states. Red flag: using two coroutines mutating shared state.
How do you share a ViewModel across multiple Fragments?
Tests ViewModel scoping and Fragment communication. A strong answer names activityViewModels or navGraphViewModels, warns about Activity memory retention and hidden coupling, and suggests a parent Fragment or data flow alternative.
Why is LiveData<Boolean> problematic for one-time ViewModel events?
State is not events; suggest an Event wrapper with a consumed flag, or SharedFlow or Channel with no replay. LiveData replays one-time events after rotation. Praising SingleLiveEvent or onCleared resets as fix.
Explain the roles of ViewModel, Repository, and data source in MVVM
Tests your grasp of unidirectional data flow and separation of concerns in Android MVVM. A strong answer maps ViewModel to UI state, Repository to data coordination, and Room or Retrofit to I/O.
What is a ViewModel and what lifecycle problem does it solve?
Tests config-change survival: ViewModel outlives Activity recreation so state survives rotation. Strong answer notes it holds non-serializable data and separates concerns, but does not survive process death.
Your app is killed for memory; what runs when the user returns?
Tests process death versus config change. Answer: no callback on kill; return runs onCreate with savedInstanceState, onStart, and onResume. ViewModels die in process death; use SavedStateHandle. Red flag: claiming onDestroy fires or ViewModels survive it.
How do you share a ViewModel between Fragments?
Tests your understanding of ViewModel lifecycle scoping beyond a single screen. To share, scope the ViewModel to the parent Activity or a navigation graph using `activityViewModels()` or `navGraphViewModels()`.
Why is LiveData<Boolean> bad for one-time ViewModel events?
Tests if you know LiveData holds state, not events. A good answer explains that LiveData re-emits on configuration changes, causing repeated events. The solution is to model events as part of the UI state and consume them.
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.
Explain app restoration after Android process death
Tests your grasp of process death vs. configuration changes. A good answer explains that the entire app process is recreated, starting with onCreate, and state must be restored from the SavedStateHandle.
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 happens to an Activity and its text on screen rotation?
Tests understanding of configuration changes and state preservation. A great answer explains the Activity is destroyed and recreated, `EditText` state is often saved automatically, but the robust solution is using a `ViewModel` with `SavedStateHandle`.
Why is `LiveData<Boolean>` bad for one-time ViewModel events?
This tests your grasp of state vs. events. A `LiveData<Boolean>` is state; it re-triggers on rotation. Instead, expose events via a `StateFlow` and have the UI call an `eventConsumed()` function on the ViewModel to maintain unidirectional data flow.
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.
How does a ViewModel survive configuration changes?
Tests understanding of ViewModel's scope, separate from the Activity lifecycle. A ViewModel is retained by a ViewModelStore, which persists across configuration changes. The new Activity instance then reconnects to the same ViewModel.
How do you save UI state during screen rotation?
Tests your grasp of the Activity lifecycle and state restoration. A great answer explains the destroy/recreate cycle, notes that EditText handles its own state via its ID, and contrasts the modern ViewModel + SavedStateHandle with the older…
@HiltViewModel: Simplified ViewModel Injection
@HiltViewModel automates creating ViewModels with dependencies. Instead of writing custom factories, just annotate the ViewModel and its constructor. It's the standard for injecting dependencies like repositories into ViewModels in a Hilt-powered Android app.
Android's UI Layer: Displaying Data, Handling Events
The UI layer is what users see and touch. It displays app data on screen and sends user input like taps to your business logic. It's used in every screen, from login forms to media players.
Get Viewmodel bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.