0Pricing
Kotlin Multiplatform Academy · Lesson

A Cross-Platform ViewModel Base

Hold state and a scope that both platforms can use.

A Cross-Platform ViewModel Base is a free Kotlin Multiplatform 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 Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🚀

Frequently asked questions

Is the “A Cross-Platform ViewModel Base” lesson free?

Yes — the full text of “A Cross-Platform ViewModel Base” is free to read here on the web, and the Kotlin Multiplatform 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 Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “A Cross-Platform ViewModel Base”?

Hold state and a scope that both platforms can use. You practise Kotlin Multiplatform 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 Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform 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 “A Cross-Platform ViewModel Base” 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 Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform 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. A Cross-Platform ViewModel Base
  2. Expose UI State as StateFlow
  3. Handle User Intents & Actions
  4. Bind the ViewModel to Each UI
← Back to Kotlin Multiplatform Academy