0Pricing
Kotlin Multiplatform Academy · Lektion

Eine plattformübergreifende ViewModel-Basisklasse

Zustand und einen Scope verwalten, den beide Plattformen nutzen können

Eine plattformübergreifende ViewModel-Basisklasse ist eine kostenlose Kotlin Multiplatform 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 Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.

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

Why Share the ViewModel?

A ViewModel holds screen state and logic. In KMP you write it once in shared code so both apps stay perfectly in sync. 🎉

Logic, Not Pixels

Your shared ViewModel decides what the screen shows. The native UI decides how to draw it. That split is the heart of KMP.

A Plain Class to Start

At its core a ViewModel is just a normal Kotlin class living in commonMain. No magic, no framework required to begin.

class CounterViewModel {
    var count = 0
}

It Needs a Scope

Async work needs a CoroutineScope. Every ViewModel keeps one so it can launch coroutines and cancel them later cleanly.

val scope = CoroutineScope(
    SupervisorJob() + Dispatchers.Main
)

SupervisorJob Keeps Siblings Alive

Using a SupervisorJob means one failing coroutine will not cancel the others in the same scope. Failures stay isolated.

A Reusable Base Class

Put the shared scope in an open base class so every screen's ViewModel inherits the same lifecycle plumbing for free.

open class BaseViewModel {
    protected val scope =
        CoroutineScope(SupervisorJob() + Dispatchers.Main)
}

Always Provide Cleanup

Add a clear() method that cancels the scope. When the screen goes away, the platform calls it so no work leaks.

open fun clear() {
    scope.cancel()
}

Extend the Base

Now a real screen just extends the base. It gets a ready scope and clean shutdown without repeating the boilerplate.

class HomeViewModel : BaseViewModel() {
    // screen logic here
}

androidx Has Its Own ViewModel

KMP now ships a multiplatform androidx.lifecycle.ViewModel. It gives you a managed viewModelScope on every target.

viewModelScope for Free

Extend that ViewModel and you get a built-in viewModelScope that is cancelled automatically when the ViewModel clears.

class HomeViewModel : ViewModel() {
    fun load() = viewModelScope.launch { }
}

One Class, Both Platforms

Whichever base you pick, the same shared ViewModel drives Android and iOS. Write the behavior once, trust it everywhere.

Quick Check

Let's lock in why a ViewModel carries a scope.

Recap

A shared ViewModel is a plain Kotlin class with a CoroutineScope and a clear() to cancel it. Build once, reuse on both apps. 🚀

Häufig gestellte Fragen

Ist die Lektion „Eine plattformübergreifende ViewModel-Basisklasse“ kostenlos?

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

Was lerne ich in „Eine plattformübergreifende ViewModel-Basisklasse“?

Zustand und einen Scope verwalten, den beide Plattformen nutzen können Du übst Kotlin Multiplatform 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 Kotlin Multiplatform Academy zu starten?

Keine Vorkenntnisse erforderlich. Kotlin Multiplatform 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 „Eine plattformübergreifende ViewModel-Basisklasse“?

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 Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?

Ja. Jede Kotlin Multiplatform 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. Eine plattformübergreifende ViewModel-Basisklasse
  2. UI-Zustand als StateFlow bereitstellen
  3. User-Intents und Aktionen verarbeiten
  4. Das ViewModel an jede UI binden
← Zurück zu Kotlin Multiplatform Academy