Snackbar e SnackbarHostState
Mostri messaggi temporanei secondo lo stile Material.
Snackbar e SnackbarHostState è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 4 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.
What Is a Snackbar?
A Snackbar is a brief message that slides up at the bottom, like Message sent, then fades on its own. It informs without blocking the screen. 💬
Snackbar vs Dialog
A dialog demands a response; a Snackbar does not. Reach for it for quick confirmations, not for choices the user must make.
Meet SnackbarHostState
Showing a snackbar is driven by SnackbarHostState. This object holds the queue and gives you the function that triggers a message.
val hostState = remember { SnackbarHostState() }Remember It
Wrap the state in remember so the same SnackbarHostState survives recomposition. A fresh one each frame would lose any pending message.
The snackbarHost Slot
Tell Scaffold where snackbars appear by filling its snackbarHost slot with a SnackbarHost bound to your state.
snackbarHost = { SnackbarHost(hostState) }showSnackbar Is Suspend
The showSnackbar function suspends, because it waits while the message is visible. So you must call it from a coroutine, not directly in a click.
Launch From a Scope
Grab a rememberCoroutineScope and launch showSnackbar inside it. That bridges a normal onClick to the suspend call safely.
scope.launch {
hostState.showSnackbar("Saved")
}Add an Action
Pass an actionLabel to add a button, such as Undo. The call returns a result telling you whether the user tapped that action.
val result = hostState.showSnackbar(
"Deleted", actionLabel = "Undo"
)React to the Result
Check the returned SnackbarResult. If it equals ActionPerformed, run your undo logic; otherwise the snackbar simply dismissed.
if (result == SnackbarResult.ActionPerformed) {
restoreItem()
}Control the Duration
Set how long it lingers with SnackbarDuration: Short, Long, or Indefinite. Use Indefinite only when the message truly needs the user to act.
One Host, Many Messages
A single SnackbarHostState queues messages, showing them one at a time. So share one host per Scaffold rather than creating several.
Quick Check
Why must you launch showSnackbar inside a coroutine scope?
Recap
Remember a SnackbarHostState, wire it into Scaffold's snackbarHost, then launch showSnackbar from a scope. You can finish a polished app shell now. 🎉
Domande Frequenti
La lezione «Snackbar e SnackbarHostState» è gratuita?
Sì — il testo completo di «Snackbar e SnackbarHostState» è 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 «Snackbar e SnackbarHostState»?
Mostri messaggi temporanei secondo lo stile Material. 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 4 di 4.
Quanto tempo richiede la lezione «Snackbar e SnackbarHostState»?
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
- Scaffold: lo scheletro della schermata
- TopAppBar e icone delle azioni
- Barre inferiori e pulsanti Floating Action
- Snackbar e SnackbarHostState