What is MVVM?

In short
MVVM stands for Model-View-ViewModel, an app design pattern that splits an app into three parts. The View shows the screen, the ViewModel holds the state and logic for that screen, and the Model holds the data. In Android, this split keeps data safe when the system recreates screens.
The three parts of MVVM
MVVM splits an app into three roles. The View is what the user sees and touches, such as a screen with buttons and lists. The ViewModel sits between the view and the rest of the app. It holds the state a screen needs and handles the logic that decides what that state should be. The Model is the data of the app and the code that loads, stores and changes it.
The main idea is that the view should not own the data. Views are easy to lose. A screen can be destroyed and rebuilt when the device rotates, when a foldable opens or closes, or when the system needs memory. If the data lived only in the screen, it would vanish with it.
How it works in Android
The Android documentation describes a related approach. It recommends at least two layers: a UI layer and a data layer. The UI layer displays application data. The data layer holds the business logic and exposes the data. MVVM maps well onto this split. The ViewModel is the state holder in the UI layer, and the repositories in the data layer are the Model.
The documentation says state holders should live as long as the UI they serve. A ViewModel for a screen should stay in memory until that screen leaves the navigation back stack. This is why a ViewModel survives a rotation while the activity is recreated. The view observes the ViewModel and redraws whenever its state changes.
Data should flow in one direction. State moves from the ViewModel to the view. User events, such as a button press, move from the view to the ViewModel. The ViewModel then asks the model to change the data. This one-way flow, often called unidirectional data flow, makes bugs easier to trace. You can read more in the architecture guide on developer.android.com.
When to use it
MVVM fits screens with state that must survive configuration changes, and apps that need to be tested without running the UI. Because the ViewModel holds no view references, you can test its logic with plain unit tests. It also suits apps that work offline, because the model can read from a local database and the view simply reflects what the model provides.
MVVM may be too much for a very small app with one screen and little data. In that case, a simple structure works fine. The documentation notes that a domain layer is optional and should be added only when the logic is complex or reused by several ViewModels.
A simple example
Imagine a screen that shows a list of notes. The model is a repository that loads notes from a database. The ViewModel asks the repository for the notes and exposes them as state. The view draws that state.
class NotesViewModel(private val repository: NotesRepository) : ViewModel() {
val notes: StateFlow<List<Note>> = repository.observeNotes()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
fun addNote(text: String) {
viewModelScope.launch { repository.add(Note(text)) }
}
}
The screen only reads notes and calls addNote. It never writes to the database itself. If the device rotates, the ViewModel keeps its state and the new screen reads the same data again.
Common mistakes
A frequent mistake is putting the data logic inside the view. The screen then holds the notes and the database calls, which makes it hard to test and easy to lose data. Another mistake is letting the ViewModel reference views or activities. This creates memory leaks and couples the logic to the UI. A third mistake is exposing mutable state. Keep the state immutable to the view, and let the ViewModel be the only place that changes it.
Alternatives
MVVM is not the only option. Model-View-Presenter (MVP) and Model-View-Intent (MVI) are other patterns that separate UI from logic. In modern Jetpack Compose apps, a state holder that is a plain class can also replace a ViewModel for simple cases. The right choice depends on the size of the app and the team's experience.
FAQ
Is MVVM required for Android apps?
No. The Android documentation recommends a layered architecture, but it does not require a specific pattern. MVVM is one common way to build the UI layer.
Why does the ViewModel survive screen rotation?
The ViewModel is kept in memory for as long as its screen stays in the navigation back stack. The activity can be recreated during rotation, but the ViewModel is not destroyed with it.
Can a ViewModel store all my app data?
It should not. Data should live in a single source of truth, which is often a database. The ViewModel holds the state a screen needs and reads from that source.
What is the difference between MVVM and MVC?
In MVC, the controller handles input and updates the view directly. In MVVM, the view observes the ViewModel's state, so the two are less tightly connected.
Sources
This page is based on the official documentation below. Read it for full detail.
MVVM — official documentation ↗