0Pricing
Jetpack Compose Academy · 课时

Snackbars 与 SnackbarHostState

以 Material 方式显示临时消息。

Snackbars 与 SnackbarHostState 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「Snackbars 与 SnackbarHostState」课时是免费的吗?

是的 — 「Snackbars 与 SnackbarHostState」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。

「Snackbars 与 SnackbarHostState」这节课中我会学到什么?

以 Material 方式显示临时消息。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Jetpack Compose Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Snackbars 与 SnackbarHostState」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?

能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Scaffold:屏幕骨架
  2. TopAppBar 与操作图标
  3. 底部栏与浮动操作按钮
  4. Snackbars 与 SnackbarHostState
← 返回 Jetpack Compose Academy