Jetpack Compose Snackbar — Complete Guide
In this tutorial, you'll learn about Jetpack Compose Snackbar. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your snackbar appears behind the bottom bar, multiple snackbars queue incorrectly, or the action button doesn't respond.
Wrong Approach ❌
@Composable
fun BadSnackbar() {
Scaffold {
// Snackbar triggered without SnackbarHost
LaunchedEffect(Unit) {
// No SnackbarHostState — snackbar never shows
Snackbar { Text("Message") }
}
}
}
// Multiple concurrent snackbars
@Composable
fun ManySnackbars() {
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
Scaffold(snackbarHost = { SnackbarHost(snackbarHostState) }) {
Column {
Button(onClick = {
scope.launch { snackbarHostState.showSnackbar("One") }
scope.launch { snackbarHostState.showSnackbar("Two") }
}) { Text("Show") }
}
}
}
Output: Snackbar invisible. Second snackbar replaces first immediately.
Right Approach ✅
@Composable
fun GoodSnackbar() {
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
Scaffold(
snackbarHost = {
SnackbarHost(hostState = snackbarHostState) { data ->
Snackbar(
snackbarData = data,
containerColor = MaterialTheme.colorScheme.inverseSurface,
contentColor = MaterialTheme.colorScheme.inverseOnSurface,
actionColor = MaterialTheme.colorScheme.primary,
shape = RoundedCornerShape(8.dp)
)
}
}
) { innerPadding ->
Column(modifier = Modifier.padding(innerPadding)) {
Button(onClick = {
scope.launch {
val result = snackbarHostState.showSnackbar(
message = "Item deleted",
actionLabel = "Undo",
duration = SnackbarDuration.Short // Long, Indefinite
)
if (result == SnackbarResult.ActionPerformed) {
// Undo deletion
}
}
}) {
Text("Delete")
}
}
}
}
Output: Properly positioned snackbar with working action button.
Prevention
- Always pass a
SnackbarHostStatetoSnackbarHostin Scaffold. - Use
rememberCoroutineScope()to launch snackbar from callbacks. - Handle
SnackbarResultfromshowSnackbar()for action callbacks. - Use
SnackbarDuration.Indefinitefor persistent messages that need user action.
Common Mistakes with compose snackbar
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro