0Pricing
Jetpack Compose Academy · Lezione

StateFlow e SharedFlow a confronto

Scelga il flusso hot corretto per l'interfaccia.

StateFlow e SharedFlow a confronto è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «StateFlow e SharedFlow a confronto» è gratuita?

Sì — il testo completo di «StateFlow e SharedFlow a confronto» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Cosa imparerò in «StateFlow e SharedFlow a confronto»?

Scelga il flusso hot corretto per l'interfaccia. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Jetpack Compose Academy?

Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «StateFlow e SharedFlow a confronto»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?

Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. StateFlow e SharedFlow a confronto
  2. Mappare e combinare i Flow
  3. stateIn e WhileSubscribed
  4. Eventi una tantum con i Channel
← Torna a Jetpack Compose Academy