0% found this document useful (0 votes)
26 views4 pages

Dialog Destinations - Android Developers

Uploaded by

Jeebers Crrebers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Dialog Destinations - Android Developers

Uploaded by

Jeebers Crrebers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Dialog destinations | Android Developers https://developer.android.com/guide/navigation/design/dia...

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 (/guide/navigation/design) that


�ll the navigation host, there are some impo�ant considerations regarding how dialog
destinations interact with your 's back stack
(/guide/navigation/backstack/dialog).

Note: Dialog destinations implement the


(/reference/androidx/navigation/FloatingWindow) inte�ace. Your app treats any destination that
implements this inte�ace as a dialog destination.

To create a dialog destination in Compose, add a destination to your using the

(/reference/kotlin/androidx/navigation/
NavGraphBuilder#(androidx.navigation.NavGraphBuilder).dialog(kotlin.collections.Map,kotlin.Function
1))
function. The function behaves essentially the same as
(/reference/kotlin/androidx/navigation/
NavGraphBuilder#(androidx.navigation.NavGraphBuilder).composable(kotlin.collections.Map,kotlin.col
lections.List,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Fun
ction2))()
, only it creates a dialog destination rather than a hosted destination
(/guide/navigation/design).

Consider the following example:

@Serializable
object Home
@Serializable
object Settings
@Composable

1 of 4 11/9/24, 12:30
Dialog destinations | Android Developers https://developer.android.com/guide/navigation/design/dia...

fun HomeScreen(onNavigateToSettings: () -> Unit){


Column {
Text("Home")
Button(onClick = onNavigateToSettings){
Text("Open settings")
}
}
}

∕∕ This screen will be displayed as a dialog


@Composable
fun SettingsScreen(){
Text("Settings")
∕∕ ...
}

@Composable
fun MyApp() {
val navController = rememberNavController()
NavHost(navController, startDestination = Home) {
composable<Home> { HomeScreen(onNavigateToSettings = { navController.navigate
dialog<Settings> { SettingsScreen() }
}
}

���The sta� destination uses the route. Because adds it to the


graph, it is a hosted destination.

���The other destination uses the route.

• Similarly, because adds it to the graph, it is a dialog destination.

• When the user navigates from to the la�er


appears over .

���Although doesn't include a composable itself, because it is


a dialog destination, the displays it within a .

Dialog destinations appear over the previous destination in the . 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 (/jetpack/compose/components/dialog) or related composable if
you want a dialog for a less complex prompt, such as a con�rmation.

2 of 4 11/9/24, 12:30
Dialog destinations | Android Developers https://developer.android.com/guide/navigation/design/dia...

Note: Because bo�om sheets in Compose are not built on , they need their own destination
type. See the Accompanist Navigation Material documentation
(h�ps://google.github.io/accompanist/navigation-material/) for an example implementation.

If you are working with fragments and you are using the Kotlin DSL
(/guide/navigation/design/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


(/reference/kotlin/androidx/navigation/
NavGraphBuilder#(androidx.navigation.NavGraphBuilder).dialog(kotlin.Int))
function to add a dialog destination that uses a fragment:

∕∕ Define destinations with serializable classes or objects


@Serializable
object Home
@Serializable
object Settings

∕∕ Add the graph to the NavController with `createGraph()`.


navController.graph = navController.createGraph(
startDestination = Home
) {
∕∕ Associate the home route with the HomeFragment.
fragment<HomeFragment, Home> {
label = "Home"
}

∕∕ Define the settings destination as a dialog using DialogFragment.


dialog<SettingsFragment, Settings> {
label = "Settings"
}
}

3 of 4 11/9/24, 12:30
Dialog destinations | Android Developers https://developer.android.com/guide/navigation/design/dia...

If you have an existing (/reference/androidx/fragment/app/DialogFragment),


use the 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">

...

...

<∕navigation>

Content and code samples on this page are subject to the licenses described in the Content License
(/license). Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its a�liates.

Last updated 2024-05-31 UTC.

4 of 4 11/9/24, 12:30

You might also like