0Pricing
Jetpack Compose Academy · Lektion

Warum ein ViewModel Rotation übersteht

Erhalten Sie State über Konfigurationsänderungen hinweg.

Warum ein ViewModel Rotation übersteht ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Warum ein ViewModel Rotation übersteht“ kostenlos?

Ja — der vollständige Text von „Warum ein ViewModel Rotation übersteht“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Warum ein ViewModel Rotation übersteht“?

Erhalten Sie State über Konfigurationsänderungen hinweg. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?

Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Warum ein ViewModel Rotation übersteht“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?

Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Warum ein ViewModel Rotation übersteht
  2. viewModel() in einer Composable
  3. Eine UiState-Data-Class modellieren
  4. collectAsStateWithLifecycle
← Zurück zu Jetpack Compose Academy