StateFlowとSharedFlow
UIに適したホットストリームを選びます。
「StateFlowとSharedFlow」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Hot Streams for the UI
To stream live data into Compose you use a hot flow that stays active and emits whether or not anyone is currently collecting it.
Meet StateFlow
A StateFlow always holds exactly one current value. New collectors immediately receive that latest value, which is perfect for screen state. 📦
val count = MutableStateFlow(0)
count.value = 1Read the Value Anytime
Because StateFlow caches one item, you can read its current value synchronously through the value property without collecting.
val ui: StateFlow<UiState> = _ui
val now = ui.valueStateFlow Skips Duplicates
A StateFlow only emits when the value actually changes. Setting it to the same value again sends nothing to collectors.
_count.value = 5
_count.value = 5 // no new emissionMeet SharedFlow
A SharedFlow broadcasts events to many collectors but holds no required current value. It is built for things that happen, not state that is.
val events = MutableSharedFlow<String>()SharedFlow Replay Buffer
A SharedFlow can replay a few past emissions to late collectors. With replay zero, new collectors only see future events.
MutableSharedFlow<Int>(replay = 1)SharedFlow Keeps Duplicates
Unlike StateFlow, a SharedFlow emits every value you send, even repeats. Two identical taps fire two separate events.
events.emit("tap")
events.emit("tap") // both deliveredState Versus Events
The rule of thumb: use StateFlow for what the screen looks like, and SharedFlow for one-time things like a navigation or a toast.
Expose Read-Only Streams
Keep a private mutable flow inside the ViewModel and expose a read-only one. The UI observes but cannot mutate it.
private val _ui = MutableStateFlow(UiState())
val ui: StateFlow<UiState> = _uiStateFlow Needs a Default
A StateFlow must start with an initial value, so the UI always has something to show. A SharedFlow can start empty.
MutableStateFlow(UiState(loading = true))Collect in Compose
You turn either flow into Compose state with a collector. For a StateFlow the screen has data immediately on first frame.
val state by viewModel.ui.collectAsStateWithLifecycle()Quick Check
Match each flow to its purpose.
Recap: StateFlow vs SharedFlow
Nice! StateFlow caches one current value for screen state, while SharedFlow broadcasts repeatable events with an optional replay buffer. 🎯
よくある質問
「StateFlowとSharedFlow」レッスンは無料ですか?
はい。「StateFlowとSharedFlow」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「StateFlowとSharedFlow」で何を学びますか?
UIに適したホットストリームを選びます。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「StateFlowとSharedFlow」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- StateFlowとSharedFlow
- Flowをマッピングして結合する
- stateInとWhileSubscribed
- Channelによる一度きりのイベント