SnackbarAction
Represents different action types that can be attached to a Snackbar message.
Values identify localized labels; the snackbar callback owns the resulting behavior. StatefulComposable requests REPORT for errors and ignores the callback's Boolean result. The app's callback reports the throwable only when that action is selected. UNDO has no built-in rollback implementation.
Illustrative ViewModel excerpt (repository and state declarations omitted)
@HiltViewModel
class MyViewModel @Inject constructor(
private val repository: MyRepository
) : ViewModel() {
fun deleteItem(id: String) {
_uiState.updateWith {
repository.deleteItem(id)
}
}
}Illustrative Screen excerpt (imports and content omitted)
@Composable
fun ExampleScreen(
onShowSnackbar: suspend (String, SnackbarAction, Throwable?) -> Boolean,
viewModel: MyViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
StatefulComposable(
state = uiState,
onShowSnackbar = onShowSnackbar
) { screenData ->
ExampleContent(
onDelete = { id ->
viewModel.deleteItem(id)
}
)
}
}Illustrative custom handler inside a composable
Imports and surrounding UI are omitted; resource lookup uses the captured Context because the suspend callback runs outside composition.
val snackbarHostState = remember { SnackbarHostState() }
val context = LocalContext.current
val onShowSnackbar: suspend (String, SnackbarAction, Throwable?) -> Boolean = { message, action, _ ->
val result = snackbarHostState.showSnackbar(
message = message,
actionLabel = if (action != SnackbarAction.NONE) {
context.getString(action.actionText)
} else {
null
},
duration = SnackbarDuration.Short
)
result == SnackbarResult.ActionPerformed
}See also
Entries
Properties
Functions
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.)
Returns an array containing the constants of this enum type, in the order they're declared.