StateFlow 与 SharedFlow
为用户界面选择合适的热流。
StateFlow 与 SharedFlow 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。
「StateFlow 与 SharedFlow」这节课中我会学到什么?
为用户界面选择合适的热流。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 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 处理一次性事件