先前的範例說明如何在導覽圖 XML 中指定 NavOptions。然而,特定選項可能會因建構時未知的限制而有所不同。在這種情況下,就必須以程式輔助方式建立及設定 NavOptions,如以下範例所示:
Kotlin
findNavController().navigate(R.id.action_fragmentOne_to_fragmentTwo,null,navOptions{// Use the Kotlin DSL for building NavOptionsanim{enter=android.R.animator.fade_inexit=android.R.animator.fade_out}})
findNavController().navigate(deepLinkUri,navOptions{// Use the Kotlin DSL for building NavOptionsanim{enter=android.R.animator.fade_inexit=android.R.animator.fade_out}})
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Navigate with options\n\nWhen you define an action in the navigation graph using the Kotlin DSL,\nNavigation generates a corresponding [`NavAction`](/reference/androidx/navigation/NavAction) class, which contains the\nconfigurations defined for that action, including the following:\n\n- **[Destination](/reference/kotlin/androidx/navigation/NavAction#getDestinationId()):** The resource ID of the target destination.\n- **[Default arguments](/reference/kotlin/androidx/navigation/NavAction#getDefaultArguments()):** An `android.os.Bundle` containing default values for the target destination, if supplied.\n- **[Navigation options](/reference/kotlin/androidx/navigation/NavAction#getNavOptions()):** Navigation options, represented as [`NavOptions`](/reference/androidx/navigation/NavOptions). This class contains all of the special configuration for transitioning to and back from the target destination, including animation resource configuration, pop behavior, and whether the destination should be launched in single top mode.\n\nOptions with Compose\n--------------------\n\nBy default, `navigate()` adds your new destination to the back stack. You can\nmodify the behavior of `navigate()` by passing additional navigation options to\nyour `navigate()` call.\n\nYou can create an instance of `NavOptions` using a simple lambda. Pass\n`navigate()` the arguments you might otherwise explicitly pass to the\n`NavOptions.Builder`. Consider the following examples:\n\nFor examples, see the [back stack guide](/guide/navigation/backstack#compose-examples) for examples on how to pass options\nto `navigate()` in context.\n| **Note:** You cannot use [`anim` block](/reference/kotlin/androidx/navigation/NavAction#getDefaultArguments()) with Navigation Compose. There is a [feature request](/reference/kotlin/androidx/navigation/NavAction#getNavOptions()) that tracks Transition Animations in Navigation Compose.\n| **Note:** The user's current location is already in the back stack. It appears in the back stack when the user first navigates to the destination, not when they navigate away.\n\nOptions with XML\n----------------\n\nThe following is an example graph consisting of two screens along with an action\nto navigate from one to the other: \n\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 xmlns:tools=\"http://schemas.android.com/tools\"\n android:id=\"@+id/nav_graph\"\n app:startDestination=\"@id/a\"\u003e\n\n \u003cfragment android:id=\"@+id/a\"\n android:name=\"com.example.myapplication.FragmentA\"\n android:label=\"a\"\n tools:layout=\"@layout/a\"\u003e\n \u003caction android:id=\"@+id/action_a_to_b\"\n app:destination=\"@id/b\"\n app:enterAnim=\"@anim/nav_default_enter_anim\"\n app:exitAnim=\"@anim/nav_default_exit_anim\"\n app:popEnterAnim=\"@anim/nav_default_pop_enter_anim\"\n app:popExitAnim=\"@anim/nav_default_pop_exit_anim\"/\u003e\n \u003c/fragment\u003e\n\n \u003cfragment android:id=\"@+id/b\"\n android:name=\"com.example.myapplication.FragmentB\"\n android:label=\"b\"\n tools:layout=\"@layout/b\"\u003e\n \u003caction android:id=\"@+id/action_b_to_a\"\n app:destination=\"@id/a\"\n app:enterAnim=\"@anim/nav_default_enter_anim\"\n app:exitAnim=\"@anim/nav_default_exit_anim\"\n app:popEnterAnim=\"@anim/nav_default_pop_enter_anim\"\n app:popExitAnim=\"@anim/nav_default_pop_exit_anim\"\n app:popUpTo=\"@+id/a\"\n app:popUpToInclusive=\"true\"/\u003e\n \u003c/fragment\u003e\n \u003c/navigation\u003e\n\nWhen the navigation graph is inflated, these actions are parsed, and\ncorresponding `NavAction` objects are generated with the configurations defined\nin the graph. For example, `action_b_to_a` is defined as navigating from\ndestination `b` to destination `a`. The action includes animations along with\n`popTo` behavior that removes all destinations from the backstack. All of these\nsettings are captured as `NavOptions` and are attached to the `NavAction`.\n\nTo follow this `NavAction`, use `NavController.navigate()`, passing the ID of\nthe action, as shown in the following example: \n\n navController.navigate(R.id.action_b_to_a)\n\n### Apply options programmatically\n\nThe previous examples show how to specify `NavOptions` within the navigation\ngraph XML. However, specific options can vary depending on constraints that are\nunknown at build time. In such cases, the `NavOptions` must be created and set\nprogrammatically, as shown in the following example: \n\n### Kotlin\n\n findNavController().navigate(\n R.id.action_fragmentOne_to_fragmentTwo,\n null,\n navOptions { // Use the Kotlin DSL for building NavOptions\n anim {\n enter = android.R.animator.fade_in\n exit = android.R.animator.fade_out\n }\n }\n )\n\n### Java\n\n NavController navController = NavHostFragment.findNavController(this);\n navController.navigate(\n R.id.action_fragmentOne_to_fragmentTwo,\n null,\n new NavOptions.Builder()\n .setEnterAnim(android.R.animator.fade_in)\n .setExitAnim(android.R.animator.fade_out)\n .build()\n );\n\nThis example uses an extended form of [`navigate()`](/reference/androidx/navigation/NavController#navigate(int,%20android.os.Bundle,%20androidx.navigation.NavOptions)) and contains additional\n`Bundle` and `NavOptions` arguments. All variants of `navigate()` have extended\nversions that accept a `NavOptions` argument.\n| **Note:** `NavOptions` that are applied programmatically override any and all options that have been set in XML.\n\nYou can also programmatically apply `NavOptions` when navigating to implicit\ndeep links: \n\n### Kotlin\n\n findNavController().navigate(\n deepLinkUri,\n navOptions { // Use the Kotlin DSL for building NavOptions\n anim {\n enter = android.R.animator.fade_in\n exit = android.R.animator.fade_out\n }\n }\n )\n\n### Java\n\n NavController navController = NavHostFragment.findNavController(this);\n navController.navigate(\n deepLinkUri,\n new NavOptions.Builder()\n .setEnterAnim(android.R.animator.fade_in)\n .setExitAnim(android.R.animator.fade_out)\n .build()\n );\n\nThis variant of [`navigate()`](/reference/androidx/navigation/NavController#navigate(android.net.Uri,%20androidx.navigation.NavOptions)) takes a [`Uri`](/reference/android/net/Uri) for the implicit deep\nlink, as well as the `NavOptions` instance."]]