0Pricing
Android Academy · Lesson

Why Use a ViewModel?

Survive configuration changes cleanly.

What Is a ViewModel?

A ViewModel is an Android architecture component designed to store and manage UI-related data in a lifecycle-conscious way.

It survives configuration changes such as screen rotations, so your data does not get destroyed and recreated every time the system rebuilds the UI.

The Configuration Change Problem

When you rotate the device, Android destroys and recreates the Activity. Any state held inside the Activity or Composable is lost.

  • A counter resets to zero.
  • A loaded list disappears.
  • A network request might restart.

ViewModel solves this by living outside the Activity instance lifecycle.

All lessons in this course

  1. Why Use a ViewModel?
  2. Exposing State from ViewModel
  3. The Android Lifecycle
  4. collectAsStateWithLifecycle
← Back to Android Academy