In Android navigation, the term dialog destination refers to destinations within the app's navigation graph which take the form of dialog windows, overlaying app UI elements and content.
Because dialog destinations appear over hosted destinations that fill
the navigation host, there are some important considerations regarding how
dialog destinations interact with your NavController
's back stack.
Dialog composable
To create a dialog destination in Compose, add a destination to your NavHost
using the dialog()
function. The function behaves essentially the same as
composable()
, only it creates a dialog destination rather than a hosted
destination.
Consider the following example:
@Composable
fun SettingsDialog(){
Text("Settings")
// ...
}
@Composable
fun MyApp() {
val navController = rememberNavController()
NavHost(navController, startDestination = "home") {
composable("home") { Home(onNavigateToHome = { navController.navigate("home") }) }
dialog("settings") { SettingsDialog(onNavigateToSettingsDialog = { navController.navigate("settings") }) }
}
}
- The start destination is the
Home
composable. Because it usescomposable()
, it is a hosted destination. - The other destination is the
SettingsDialog
composable. Because thedialog()
function adds it to the graph, it is a dialog destination. When the user navigates fromHome
toSettingsDialog
, the latter appears overHome
. - Although
SettingsDialog
doesn't include aDialog
composable itself, because it is a dialog destination, theNavHost
displays it within aDialog
.
Dialog destinations appear over the previous destination in the NavHost
.
Use them when the dialog represents a separate screen in your app that
needs its own lifecycle and saved state, independent of any other destination in
your navigation graph. You might prefer to use an AlertDialog
or related
composable if you want a dialog for a less complex prompt, such as a
confirmation.
Kotlin DSL
If you are working with fragments and you are using the Kotlin DSL to create your graph, adding a dialog destination is very similar to when using Compose.
Consider how in the following snippet also uses the dialog()
function to
add a dialog destination that uses a fragment:
// Add the graph to the NavController with `createGraph()`.
navController.graph = navController.createGraph(
startDestination = "home"
) {
// Associate the "home" destination with the HomeFragment.
fragment<HomeFragment>("home") {
label = "Home"
}
// Define the "settings" destination as a dialog using DialogFragment.
dialog<SettingsDialogFragment>("settings") {
label = "Settings Dialog"
}
}
XML
If you have an existing DialogFragment
, use the <dialog>
element to add the dialog to your navigation graph, as shown in the following
example:
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph"> ... <dialog android:id="@+id/my_dialog_fragment" android:name="androidx.navigation.myapp.MyDialogFragment"> <argument android:name="myarg" android:defaultValue="@null" /> <action android:id="@+id/myaction" app:destination="@+id/another_destination"/> </dialog> ... </navigation>