Jetpack Compose Dialog — Complete Guide
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
AlertDialogfor standard confirm/cancel patterns. - Set
usePlatformDefaultWidth = falsefor full-width dialogs. - Keep dialog state (show/hide) close to the dialog composable.
- Use
DialogPropertiesto configure dismiss behavior.
Common Mistakes with compose dialog
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro