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)
}
}
}Content copied to clipboard
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)
}
)
}
}Content copied to clipboard
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
}Content copied to clipboard
See also
Entries
Properties
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.