Channelによる一度きりのイベント
ナビゲーションやトーストのイベントを安全に配信します。
「Channelによる一度きりのイベント」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎯
よくある質問
「Channelによる一度きりのイベント」レッスンは無料ですか?
はい。「Channelによる一度きりのイベント」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「Channelによる一度きりのイベント」で何を学びますか?
ナビゲーションやトーストのイベントを安全に配信します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Channelによる一度きりのイベント」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- StateFlowとSharedFlow
- Flowをマッピングして結合する
- stateInとWhileSubscribed
- Channelによる一度きりのイベント