Skip to content

Jetpack Compose Dialog — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jetpack Compose Dialog. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your dialog dismisses when you tap outside it, shows behind the system bars, or recomposes the entire screen when open.

Wrong Approach ❌

@Composable
fun BadDialog() {
    var showDialog by remember { mutableStateOf(true) }
    // Dialog inside composition — blocks all recomposition below it
    if (showDialog) {
        Dialog(onDismissRequest = { showDialog = false }) {
            // No content — empty dialog box
        }
    }
}
// Mutable state used inside dialog — triggers recomposition of parent
@Composable
fun DialogWithState() {
    var count by remember { mutableStateOf(0) }
    Dialog(onDismissRequest = {}) {
        Text("Count: $count")
        Button(onClick = { count++ }) { Text("+") }
    }
}

Output: Dialog with no visible content. Parent recomposes on every button click.

Right Approach ✅

@Composable
fun GoodDialog() {
    var showDialog by remember { mutableStateOf(false) }
    var dialogResult by remember { mutableStateOf<String?>(null) }

    Button(onClick = { showDialog = true }) {
        Text("Show Dialog")
    }

    if (showDialog) {
        AlertDialog(
            onDismissRequest = { showDialog = false },
            title = { Text("Confirm Delete") },
            text = { Text("Are you sure you want to delete this item?") },
            confirmButton = {
                Button(onClick = {
                    dialogResult = "Confirmed"
                    showDialog = false
                }) { Text("Delete") }
            },
            dismissButton = {
                OutlinedButton(onClick = {
                    dialogResult = "Cancelled"
                    showDialog = false
                }) { Text("Cancel") }
            },
            icon = { Icon(Icons.Default.Warning, "Warning") },
            shape = RoundedCornerShape(16.dp),
            tonalElevation = 6.dp
        )
    }
}

// Custom dialog content
if (showCustomDialog) {
    Dialog(
        onDismissRequest = { showCustomDialog = false },
        properties = DialogProperties(
            dismissOnBackPress = true,
            dismissOnClickOutside = true,
            usePlatformDefaultWidth = false // full-width dialog
        )
    ) {
        Surface(
            modifier = Modifier.fillMaxWidth().padding(16.dp),
            shape = RoundedCornerShape(16.dp)
        ) {
            CustomFormContent(onDismiss = { showCustomDialog = false })
        }
    }
}

Output: Properly structured dialog with correct dismiss behavior.

Prevention

  • Use AlertDialog for standard confirm/cancel patterns.
  • Set usePlatformDefaultWidth = false for full-width dialogs.
  • Keep dialog state (show/hide) close to the dialog composable.
  • Use DialogProperties to configure dismiss behavior.

Common Mistakes with compose dialog

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

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

### What is the difference between Dialog and AlertDialog?

Dialog is a generic container — you provide all content. AlertDialog has predefined slots (title, text, confirm, dismiss) following Material Design guidelines.

### How do I prevent dialog dismiss on outside click?

Set DialogProperties(dismissOnClickOutside = false). The dialog can only be dismissed via onDismissRequest or back button.

### Can I show a dialog without an Activity context?

In Compose, Dialog uses LocalContext internally. It works as long as there's a valid context in the composition tree. Don't use it in previews.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro