0Pricing
Jetpack Compose Academy · Lesson

Why ViewModel Survives Rotation

Keep state through configuration changes.

Why ViewModel Survives Rotation is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Rotation Problem

Rotate the screen and Android destroys your Activity, wiping any state you stored in it. Your counter, text, and scroll position all vanish. 😵

Configuration Changes

Rotation is one of many configuration changes: theme switches, locale changes, and window resizing all recreate your screen the same way.

Enter the ViewModel

A ViewModel is a class built to hold UI state that outlives the Activity it serves, so a rotation no longer throws your data away.

class CounterViewModel : ViewModel() {
    var count = 0
}

Tied to the Screen, Not the Activity

The ViewModel is scoped to the screen’s lifecycle owner, so the same instance is handed back to the recreated Activity instead of a fresh one.

How It Survives

Android keeps the ViewModel in a retained store across the configuration change, then reconnects it to your brand-new Activity automatically.

When It Is Cleared

A ViewModel lives until the screen is truly finished. When the user navigates away for good, onCleared() runs so you can release resources.

override fun onCleared() {
    // cancel jobs, close streams
}

Not for Everything

The ViewModel survives rotation but not process death. For that, pair it with SavedStateHandle so critical values can be restored later.

Add the Dependency

You get ViewModel support from the lifecycle libraries. Add the lifecycle-viewmodel-compose artifact so Compose can obtain one easily.

implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0")

Separation of Concerns

Beyond survival, a ViewModel keeps logic out of your UI. Your Composables stay focused on drawing while the ViewModel owns the state.

A Mental Model

Think of the ViewModel as a quiet backstage manager: the Activity comes and goes, but the manager keeps your show’s state ready to perform again.

Why This Matters

Surviving rotation means users never lose progress mid-task. That reliability is the foundation every robust Compose app is built on. ✨

Quick Check

Test your grasp of why a ViewModel outlives a rotation.

Recap

Rotation recreates your Activity, but a ViewModel survives configuration changes and reattaches, keeping UI state intact and logic out of the UI.

Frequently asked questions

Is the “Why ViewModel Survives Rotation” lesson free?

Yes — the full text of “Why ViewModel Survives Rotation” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why ViewModel Survives Rotation”?

Keep state through configuration changes. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why ViewModel Survives Rotation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why ViewModel Survives Rotation
  2. viewModel() in a Composable
  3. Modeling a UiState Data Class
  4. collectAsStateWithLifecycle
← Back to Jetpack Compose Academy