SnackbarAction

Represents different action types that can be attached to a Snackbar message.

This enum provides standardized action buttons for Snackbars, enabling users to respond to notifications or messages. Actions are automatically handled by StatefulComposable when errors occur, and can be used in custom snackbar implementations.

Usage in ViewModels

@HiltViewModel
class MyViewModel @Inject constructor(
private val repository: MyRepository
) : ViewModel() {
fun deleteItem(id: String) {
_uiState.updateWith {
repository.deleteItem(id)
}
}
}

Usage in UI Layer

@Composable
fun MyRoute(
onShowSnackbar: suspend (String, SnackbarAction, Throwable?) -> Boolean,
viewModel: MyViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

StatefulComposable(
state = uiState,
onShowSnackbar = onShowSnackbar
) { screenData ->
MyScreen(
onDelete = { id ->
viewModel.deleteItem(id)
}
)
}
}

Custom Snackbar Handler

val snackbarHostState = remember { SnackbarHostState() }

val onShowSnackbar: suspend (String, SnackbarAction, Throwable?) -> Boolean = { message, action, _ ->
val result = snackbarHostState.showSnackbar(
message = message,
actionLabel = if (action != SnackbarAction.NONE) {
stringResource(action.actionText)
} else {
null
},
duration = SnackbarDuration.Short
)
result == SnackbarResult.ActionPerformed
}

See also

Entries

Link copied to clipboard

No action button is shown on the Snackbar.

Link copied to clipboard

Shows a "Report" action button.

Link copied to clipboard

Shows an "Undo" action button.

Properties

Link copied to clipboard

String resource ID for the action button text

Link copied to clipboard

Returns a representation of an immutable list of all enum entries, in the order they're declared.

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Link copied to clipboard

Returns an array containing the constants of this enum type, in the order they're declared.