0Pricing
Jetpack Compose Academy · บทเรียน

StateFlow กับ SharedFlow

เลือกกระแสข้อมูลร้อนที่เหมาะกับส่วนติดต่อผู้ใช้

StateFlow กับ SharedFlow เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 = 1

Read 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.value

StateFlow 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 emission

Meet 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 delivered

State 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> = _ui

StateFlow 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “StateFlow กับ SharedFlow”

เลือกกระแสข้อมูลร้อนที่เหมาะกับส่วนติดต่อผู้ใช้ คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “StateFlow กับ SharedFlow” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. StateFlow กับ SharedFlow
  2. แมปและรวม Flow
  3. stateIn และ WhileSubscribed
  4. เหตุการณ์ครั้งเดียวด้วย Channel
← กลับไปที่ Jetpack Compose Academy