One-Off Events with Channels
Deliver navigation and toast events safely.
One-Off Events with Channels is a free Jetpack Compose Academy lesson on CoddyKit — lesson 4 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.
State Is Not an Event
Some things should happen exactly once, like navigating or showing a toast. Modeling these as state causes them to replay on every rotation.
The Replay Problem
If you store a snackbar message in StateFlow, a configuration change re-reads it and the message pops up again unexpectedly.
Meet the Channel
A Channel is a hot pipe where each value is delivered to exactly one receiver and then consumed, never replayed.
private val _events = Channel<UiEvent>()Send Versus Emit
You push into a Channel with send, a suspend call. The event waits in the buffer until something is ready to receive it.
_events.send(UiEvent.NavigateBack)Expose as a Flow
Turn the channel into a stream for the UI with receiveAsFlow, which lets exactly one collector consume each event.
val events = _events.receiveAsFlow()Consumed Once, Forever
Once a collector takes an event from the Channel, it is gone. A later rotation cannot read it again, so no accidental replay.
Model Events as a Type
Define a sealed UiEvent type so every one-off action, like navigation or messages, is explicit and exhaustive.
sealed interface UiEvent {
data class ShowToast(val msg: String) : UiEvent
}Collect Events in Compose
Collect the event flow inside a LaunchedEffect so it ties to the screen lifecycle and runs each event handler once.
LaunchedEffect(Unit) {
events.collect { handle(it) }
}Choose the Buffer Policy
A default Channel suspends the sender if no one receives. You can give it a buffer so sends never block your logic.
Channel<UiEvent>(Channel.BUFFERED)SharedFlow Is an Alternative
A SharedFlow with replay zero can also carry events, but a Channel guarantees single delivery even with no active collector.
Keep State and Events Apart
The clean pattern: one StateFlow for screen state and one Channel for fire-once events. They never get confused again.
Quick Check
Decide what fits a one-off action.
Recap: One-Off Events
Awesome! A Channel delivers fire-once events like navigation and toasts exactly once, keeping them separate from replayable screen state. 🎯
Frequently asked questions
Is the “One-Off Events with Channels” lesson free?
Yes — the full text of “One-Off Events with Channels” 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 “One-Off Events with Channels”?
Deliver navigation and toast events safely. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “One-Off Events with Channels” 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
- StateFlow vs SharedFlow
- Mapping & Combining Flows
- stateIn & WhileSubscribed
- One-Off Events with Channels