0Pricing
Kotlin Multiplatform Academy · Lesson

Handle User Intents & Actions

Drive state changes from UI events.

Handle User Intents & Actions is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 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.

The UI Asks, You Decide

When a user taps a button, the UI should not run logic itself. It sends an intent to the ViewModel, which decides what happens.

Name Your Intents

Model each possible user action as a sealed class. Now every intent the screen can fire is named, typed and impossible to misspell.

sealed class HomeIntent {
    object Refresh : HomeIntent()
    data class Search(val q: String) : HomeIntent()
}

One Entry Point

Expose a single onIntent function. Every screen event flows through it, giving you one tidy place to route behavior.

fun onIntent(intent: HomeIntent) {
    // route to the right handler
}

when Routes Each Action

Inside onIntent, a when matches each intent type. Because the class is sealed, the compiler ensures you cover every case.

when (intent) {
    HomeIntent.Refresh -> refresh()
    is HomeIntent.Search -> search(intent.q)
}

Handlers Update State

Each handler does its work, then updates the StateFlow. The UI re-renders from that single state, exactly as before.

private fun refresh() = scope.launch {
    _state.update { it.copy(isLoading = true) }
}

Why Not Public Methods?

You could expose many public methods, but one intent channel makes the screen's whole contract visible in one type. Clean and testable.

Carry Data in the Intent

Intents that need input use a data class. The user's query or id rides along inside the action itself.

data class Search(val query: String) : HomeIntent()

One-Off Events Are Different

State persists, but a toast or navigation happens once. For these one-off events use a Channel or SharedFlow, not StateFlow.

Emit Events with a Channel

Hold a private Channel for events and expose it as a Flow. The UI collects it to show messages or navigate just once.

private val _events = Channel<String>()
val events = _events.receiveAsFlow()

Both Apps, Same Flow

Android sends intents from Compose, iOS from SwiftUI, but they hit the same onIntent. Behavior can never drift between platforms.

A Predictable Loop

Intent in, state out: this one-way loop makes the screen easy to reason about, debug and test. No hidden side paths.

Quick Check

Let's check how UI events should reach shared logic.

Recap

Model actions as a sealed intent type, route them through one onIntent, and update state from there. Use a Channel for one-off events. 🚀

Frequently asked questions

Is the “Handle User Intents & Actions” lesson free?

Yes — the full text of “Handle User Intents & Actions” 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 “Handle User Intents & Actions”?

Drive state changes from UI events. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Handle User Intents & Actions” 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