אפשרויות ניווט: אפשרויות ניווט, שמיוצגות בתור
NavOptions. המחלקה הזו מכילה את כל ההגדרות המיוחדות עבור
מעבר ליעד היעד וחזרה ממנו, כולל אנימציה
להגדרת המשאב, ההתנהגות של הפופ, ואם היעד צריך להיות
הופעלה במצב עליון יחיד.
אפשרויות לכתיבה
כברירת מחדל, navigate() מוסיפה את היעד החדש למקבץ האחורי. אפשר
ישנה את ההתנהגות של navigate() על ידי העברת אפשרויות ניווט נוספות אל
שיחת navigate() שלך.
אפשר ליצור מופע של NavOptions באמצעות lambda פשוט. פחות בשבילי
navigate() הארגומנטים שאחרת אפשר להעביר באופן מפורש
NavOptions.Builder. מומלץ להביא בחשבון את הדוגמאות הבאות:
כשתרשים הניווט מתרחב, הפעולות האלה מנותחות
האובייקטים התואמים של NavAction נוצרים עם ההגדרות האישיות שהוגדרו
בתרשים. לדוגמה, action_b_to_a מוגדר כניווט מ-
יעד b אל היעד a. הפעולה כוללת אנימציות לצד
התנהגות של popTo שמסירה את כל היעדים מהמקבץ האחורי. כל אלה
ההגדרות מתועדות בתור NavOptions ומצורפות אל NavAction.
כדי לעקוב אחר NavAction, צריך להשתמש ב-NavController.navigate() ולהעביר את המזהה של
את הפעולה, כפי שמוצג בדוגמה הבאה:
navController.navigate(R.id.action_b_to_a)
החלת אפשרויות באופן פרוגרמטי
הדוגמאות הקודמות מראות איך לציין את NavOptions בסרגל הניווט
את ה-XML של התרשים. עם זאת, אפשרויות ספציפיות עשויות להשתנות בהתאם למגבלות
לא ידוע בזמן ה-build. במקרים כאלה, צריך ליצור ולהגדיר את 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}})
הדוגמה הזו משתמשת בצורה מורחבת של navigate() ומכילה עוד
Bundle ו-NavOptions ארגומנטים. כל הווריאציות של navigate() הוארך
גרסאות שמקבלות ארגומנט NavOptions.
אפשר גם להחיל את NavOptions באופן פרוגרמטי כשעוברים למצב משתמע
קישורי עומק:
Kotlin
findNavController().navigate(deepLinkUri,navOptions{// Use the Kotlin DSL for building NavOptionsanim{enter=android.R.animator.fade_inexit=android.R.animator.fade_out}})
הווריאנט הזה של navigate() מקבל Uri את ערך העומק המשתמע
וגם במופע NavOptions.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[[["התוכן קל להבנה","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 (שעון 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."]]