ナビゲーション グラフがインフレートされるときに、アクションが解析され、対応する NavAction オブジェクトが、グラフ内で定義されている構成に基づいて生成されます。たとえば、action_b_to_a は、デスティネーション b からデスティネーション a へのナビゲーションとして定義されます。アクションには、各種のアニメーションと、バックスタックからすべてのデスティネーションを削除する popTo 動作が含まれています。この設定はすべて NavOptions としてキャプチャされ、NavAction にアタッチされます。
この NavAction に従うには、NavController.navigate() を使用して、アクションの ID を渡します。以下の例をご確認ください。
navController.navigate(R.id.action_b_to_a)
プログラムによるオプションの適用
上述の例は、ナビゲーション グラフ 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}})
[null,null,["最終更新日 2025-07-27 UTC。"],[],[],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."]]