Thuta Learning
AdvancedMobile DevelopmentIntermediate

ViewModel & MVVM Architecture

Android DevelopmentLesson 15

What you'll walk away with

  • Understand ViewModel & MVVM Architecture without the intimidation
  • Get comfortable running Android Studio/Compose code yourself
  • Apply this concept in a real project right away

Let's think about it this way for a second

MVVM (Model-View-ViewModel) is an architecture pattern that separates the UI (View/Composable) from the business logic (ViewModel) — Model is the data (database, API), View is the UI (Composable), and ViewModel is the middleman between the two (fetching/transforming data and exposing it to the View). The `ViewModel` class survives Activity/screen rotation (an Advanced-level concept) — unlike a Composable's local state (`remember`), a ViewModel's data sticks around through configuration changes.

Let's connect this to a real scenario

If you write `TodoViewModel : ViewModel() { val todos = mutableStateListOf<TodoItem>(); fun addTodo(title: String) { todos.add(TodoItem(...)) } }` — in a Composable, grab the instance with `val viewModel: TodoViewModel = viewModel()` and display `viewModel.todos` in a `LazyColumn` — a button click just needs to call `viewModel.addTodo(...)`, no business logic mixed into the UI code anymore.

Let's walk through it together

kotlin
class TodoViewModel : ViewModel() {
    private val _todos = mutableStateListOf<TodoItem>()
    val todos: List<TodoItem> = _todos

    fun addTodo(title: String) {
        _todos.add(TodoItem(id = UUID.randomUUID().toString(), title = title))
    }
}

@Composable
fun TodoScreen(viewModel: TodoViewModel = viewModel()) {
    LazyColumn {
        items(viewModel.todos) { todo -> Text(todo.title) }
    }
}
You should see
Even after rotating the screen, you should see the todos list in the ViewModel stick around instead of disappearing.

5-minute try-it

Write `TodoViewModel` yourself and add 2-3 todo items using `addTodo()` — rotate the emulator (Ctrl+F11) and confirm the todo list doesn't disappear.

A quick word of caution

Avoid exposing state from a ViewModel as a mutable public property (`var todos = mutableListOf<...>()`) — this lets the View modify the state directly, so you should use `private set` or an immutable exposure pattern instead.

Easy traps

  • Writing business logic (data fetching, transforming) directly inside a Composable function — this hurts testing/reusability
  • Holding a Context (Activity reference) directly in a ViewModel — this risks a memory leak

Now try it yourself

Write `TodoViewModel` yourself and add 2-3 todo items using `addTodo()` — rotate the emulator (Ctrl+F11) and confirm the todo list doesn't disappear.

You'll know it worked when: Even after rotating the screen, you should see the todos list in the ViewModel stick around instead of disappearing.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

ViewModel & MVVM Architecture | Thuta Learning