Skip to content
tezvyn:

Viewmodel

25 bites tagged Viewmodel — interview questions with model answers, and 60-second explainers.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin1 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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()`.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin3 min read

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`.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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.

Android & Kotlin2 min read

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…

Android & Kotlin2 min read

@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 & Kotlin2 min read

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.