Google is committed to advancing racial equity for Black communities. See how.

SnackbarHostState

@Stable class SnackbarHostState
kotlin.Any
   ↳ androidx.compose.material.SnackbarHostState

State of the SnackbarHost, controls the queue and the current Snackbar being shown inside the SnackbarHost.

This state usually lives as a part of a ScaffoldState and provided to the SnackbarHost automatically, but can be decoupled from it and live separately when desired.

Summary

Public constructors

State of the SnackbarHost, controls the queue and the current Snackbar being shown inside the SnackbarHost.

Public methods
suspend SnackbarResult
showSnackbar(message: String, actionLabel: String? = null, duration: SnackbarDuration = SnackbarDuration.Short)

Shows or queues to be shown a Snackbar at the bottom of the Scaffold at which this state is attached and suspends until snackbar is disappeared.

Properties
SnackbarData?

The current SnackbarData being shown by the SnackbarHost, of null if none.

Public constructors

<init>

SnackbarHostState()

State of the SnackbarHost, controls the queue and the current Snackbar being shown inside the SnackbarHost.

This state usually lives as a part of a ScaffoldState and provided to the SnackbarHost automatically, but can be decoupled from it and live separately when desired.

Public methods

showSnackbar

suspend fun showSnackbar(
    message: String,
    actionLabel: String? = null,
    duration: SnackbarDuration = SnackbarDuration.Short
): SnackbarResult

Shows or queues to be shown a Snackbar at the bottom of the Scaffold at which this state is attached and suspends until snackbar is disappeared.

SnackbarHostState guarantees to show at most one snackbar at a time. If this function is called while another snackbar is already visible, it will be suspended until this snack bar is shown and subsequently addressed. If the caller is cancelled, the snackbar will be removed from display and/or the queue to be displayed.

All of this allows for granular control over the snackbar queue from within:

import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.ExtendedFloatingActionButton
import androidx.compose.material.Scaffold
import androidx.compose.material.SnackbarHostState
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.LaunchedTask
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

// decouple snackbar host state from scaffold state for demo purposes
// this state, channel and flow is for demo purposes to demonstrate business logic layer
val snackbarHostState = remember { SnackbarHostState() }
// we allow only one snackbar to be in the queue here, hence conflated
val channel = remember { BroadcastChannel<Int>(Channel.Factory.CONFLATED) }
LaunchedTask {
    channel.asFlow().collect { index ->
        val result = snackbarHostState.showSnackbar(
            message = "Snackbar # $index",
            actionLabel = "Action on $index"
        )
        when (result) {
            SnackbarResult.ActionPerformed -> {
                /* action has been performed */
            }
            SnackbarResult.Dismissed -> {
                /* dismissed, no action needed */
            }
        }
    }
}
Scaffold(
    // attach snackbar host state to the scaffold
    scaffoldState = rememberScaffoldState(snackbarHostState = snackbarHostState),
    floatingActionButton = {
        var clickCount by remember { mutableStateOf(0) }
        ExtendedFloatingActionButton(
            text = { Text("Show snackbar") },
            onClick = {
                // offset snackbar data to the business logic
                channel.offer(++clickCount)
            }
        )
    },
    bodyContent = { innerPadding ->
        Text(
            "Snackbar demo",
            modifier = Modifier.padding(innerPadding).fillMaxSize().wrapContentSize()
        )
    }
)
To change the Snackbar appearance, change it in 'snackbarHost' on the Scaffold.
Parameters
message: String text to be shown in the Snackbar
actionLabel: String? = null optional action label to show as button in the Snackbar
duration: SnackbarDuration = SnackbarDuration.Short duration to control how long snackbar will be shown in SnackbarHost, either SnackbarDuration.Short, SnackbarDuration.Long or SnackbarDuration.Indefinite
Return
SnackbarResult.ActionPerformed if option action has been clicked or SnackbarResult.Dismissed if snackbar has been dismissed via timeout or by the user

Properties

currentSnackbarData

var currentSnackbarData: SnackbarData?

The current SnackbarData being shown by the SnackbarHost, of null if none.