Skip to content

Jetpack Compose Snackbar — Complete Guide

DodaTech Updated 2026-06-24 2 min read

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 SnackbarHostState to SnackbarHost in Scaffold.
  • Use rememberCoroutineScope() to launch snackbar from callbacks.
  • Handle SnackbarResult from showSnackbar() for action callbacks.
  • Use SnackbarDuration.Indefinite for persistent messages that need user action.

Common Mistakes with compose snackbar

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. 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

### How do I show multiple snackbars in sequence?

SnackbarHostState queues snackbars internally. Call showSnackbar() sequentially — each waits for the previous to finish (or use DismissSnackbar).

### What is the difference between SnackbarDuration values?
  • Short: 4 seconds
  • Long: 10 seconds
  • Indefinite: until dismissed or action taken
### Can I customize the snackbar animation?

Override the SnackbarHost composable and wrap the Snackbar in AnimatedVisibility with custom enter/exit transitions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro