0Pricing
Jetpack Compose Academy · Lección

Eventos puntuales con Channels

Entregue eventos de navegación y toast de forma segura.

Eventos puntuales con Channels es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Jetpack Compose Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Jetpack Compose Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «Eventos puntuales con Channels» es gratis?

Sí — el texto completo de «Eventos puntuales con Channels» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Jetpack Compose Academy, actualiza a CoddyKit PRO. El curso de Jetpack Compose Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Eventos puntuales con Channels»?

Entregue eventos de navegación y toast de forma segura. Practicas Jetpack Compose Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Jetpack Compose Academy?

No se requiere experiencia previa. Jetpack Compose Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Eventos puntuales con Channels»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Jetpack Compose Academy?

Sí. Cada lección de Jetpack Compose Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. StateFlow frente a SharedFlow
  2. Mapear y combinar Flows
  3. stateIn y WhileSubscribed
  4. Eventos puntuales con Channels
← Volver a Jetpack Compose Academy