對話方塊目的地
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android Navigation 中,「對話方塊目的地」一詞是指應用程式導覽圖內的目的地,這些目的地會以對話方塊視窗的形式呈現,與應用程式 UI 元素和內容重疊。
因為對話方塊目的地會顯示在填滿
導覽主機時,請考量對話方塊和對話方塊的方式
目的地會與 NavController
的返回堆疊互動。
對話方塊可組合函式
如要在 Compose 中建立對話方塊目的地,請使用 dialog()
函式在 NavHost
中新增目的地。這個函式的行為與 composable
大致相同,唯一差別是會建立對話方塊目的地,而非代管目的地。
請參考以下範例:
@Serializable
object Home
@Serializable
object Settings
@Composable
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(route = Settings) }) }
dialog<Settings> { SettingsScreen() }
}
}
- 起始目的地使用
Home
路徑。由於
composable()
會將其新增至圖表,這是代管目的地。
- 另一個目的地使用
Settings
路徑。
- 同樣地,由於
dialog()
會將其新增至圖表,因此這是對話方塊
目的地。
- 當使用者從
HomeScreen
前往 SettingsScreen
時
後者會顯示在 HomeScreen
上方。
- 雖然
SettingsScreen
並未包含 Dialog
可組合函式本身,但這是對話方塊目的地,因此 NavHost
會在 Dialog
中顯示此函式。
對話方塊目的地會顯示在 NavHost
中的前一個目的地上方。使用
這些對話方塊代表應用程式中需要專屬畫面的獨立畫面
生命週期和已儲存狀態,與
導覽圖如果希望透過對話方塊提供較簡單的提示 (例如確認),建議您使用 AlertDialog
或相關可組合函式。
Kotlin DSL
如果您有使用片段,且運用 Kotlin DSL 建立圖表,新增對話方塊目的地的方式與使用 Compose 時非常類似。
您也可以參考下列程式碼片段,瞭解如何使用 dialog()
函式新增使用片段的對話方塊目的地:
// 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"
}
}
XML
如果已有 DialogFragment
,可使用 <dialog>
元素
將對話方塊新增至導覽圖,如以下範例所示:
<?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>
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Dialog destinations\n\nIn Android navigation, the term *dialog destination* refers to destinations\nwithin the app's navigation graph which take the form of dialog windows,\noverlaying app UI elements and content.\n\nBecause dialog destinations appear over [hosted destinations](/guide/navigation/design) that fill the\nnavigation host, there are some important considerations regarding how dialog\ndestinations interact with your [`NavController`'s back stack](/guide/navigation/backstack/dialog).\n| **Note:** Dialog destinations implement the [`FloatingWindow`](/reference/androidx/navigation/FloatingWindow) interface. Your app treats any destination that implements this interface as a dialog destination.\n\nDialog composable\n-----------------\n\nTo create a dialog destination in Compose, add a destination to your `NavHost`\nusing the [`dialog()`](/reference/kotlin/androidx/navigation/NavGraphBuilder#(androidx.navigation.NavGraphBuilder).dialog(kotlin.collections.Map,kotlin.Function1)) function. The function behaves essentially the same as\n[`composable`](/reference/kotlin/androidx/navigation/NavGraphBuilder#(androidx.navigation.NavGraphBuilder).composable(kotlin.collections.Map,kotlin.collections.List,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function2))(), only it creates a dialog destination rather than a [hosted\ndestination](/guide/navigation/design).\n\nConsider the following example: \n\n @Serializable\n object Home\n @Serializable\n object Settings\n @Composable\n fun HomeScreen(onNavigateToSettings: () -\u003e Unit){\n Column {\n Text(\"Home\")\n Button(onClick = onNavigateToSettings){\n Text(\"Open settings\")\n }\n }\n }\n\n // This screen will be displayed as a dialog\n @Composable\n fun SettingsScreen(){\n Text(\"Settings\")\n // ...\n }\n\n @Composable\n fun MyApp() {\n val navController = rememberNavController()\n NavHost(navController, startDestination = Home) {\n composable\u003cHome\u003e { HomeScreen(onNavigateToSettings = { navController.navigate(route = Settings) }) }\n dialog\u003cSettings\u003e { SettingsScreen() }\n }\n }\n\n1. The start destination uses the `Home` route. Because `composable()` adds it to the graph, it is a hosted destination.\n2. The other destination uses the `Settings` route.\n - Similarly, because `dialog()` adds it to the graph, it is a dialog destination.\n - When the user navigates from `HomeScreen` to `SettingsScreen` the latter appears over `HomeScreen`.\n3. Although `SettingsScreen` doesn't include a `Dialog` composable itself, because it is a dialog destination, the `NavHost` displays it within a `Dialog`.\n\nDialog destinations appear over the previous destination in the `NavHost`. Use\nthem when the dialog represents a separate screen in your app that needs its own\nlifecycle and saved state, independent of any other destination in your\nnavigation graph. You might prefer to use an [`AlertDialog`](/jetpack/compose/components/dialog) or related\ncomposable if you want a dialog for a less complex prompt, such as a\nconfirmation.\n| **Note:** Because bottom sheets in Compose are not built on `Dialog`, they need their own destination type. See the [Accompanist Navigation Material\n| documentation](https://google.github.io/accompanist/navigation-material/) for an example implementation.\n\nKotlin DSL\n----------\n\nIf you are working with fragments and you are using the [Kotlin DSL](/guide/navigation/design/kotlin-dsl) to\ncreate your graph, adding a dialog destination is very similar to when using\nCompose.\n\nConsider how in the following snippet also uses the [`dialog()`](/reference/kotlin/androidx/navigation/NavGraphBuilder#(androidx.navigation.NavGraphBuilder).dialog(kotlin.Int)) function to\nadd a dialog destination that uses a fragment: \n\n // Define destinations with serializable classes or objects\n @Serializable\n object Home\n @Serializable\n object Settings\n\n // Add the graph to the NavController with `createGraph()`.\n navController.graph = navController.createGraph(\n startDestination = Home\n ) {\n // Associate the home route with the HomeFragment.\n fragment\u003cHomeFragment, Home\u003e {\n label = \"Home\"\n }\n\n // Define the settings destination as a dialog using DialogFragment.\n dialog\u003cSettingsFragment, Settings\u003e {\n label = \"Settings\"\n }\n }\n\nXML\n---\n\nIf you have an existing [`DialogFragment`](/reference/androidx/fragment/app/DialogFragment), use the `\u003cdialog\u003e` element to\nadd the dialog to your navigation graph, as shown in the following example: \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cnavigation xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:id=\"@+id/nav_graph\"\u003e\n\n...\n\n\u003cdialog\n android:id=\"@+id/my_dialog_fragment\"\n android:name=\"androidx.navigation.myapp.MyDialogFragment\"\u003e\n \u003cargument android:name=\"myarg\" android:defaultValue=\"@null\" /\u003e\n \u003caction\n android:id=\"@+id/myaction\"\n app:destination=\"@+id/another_destination\"/\u003e\n\u003c/dialog\u003e\n\n...\n\n\u003c/navigation\u003e\n```"]]