با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
هنگامی که با استفاده از Kotlin DSL اقدامی را در نمودار ناوبری تعریف میکنید، Navigation یک کلاس NavAction مربوطه را تولید میکند که شامل پیکربندیهای تعریفشده برای آن عمل، از جمله موارد زیر است:
آرگومانهای پیشفرض : یک android.os.Bundle حاوی مقادیر پیشفرض برای مقصد هدف، در صورت ارائه.
گزینههای پیمایش : گزینههای پیمایش، که به عنوان NavOptions نشان داده میشوند. این کلاس شامل تمام پیکربندیهای ویژه برای انتقال به مقصد و بازگشت از مقصد است، از جمله پیکربندی منبع انیمیشن، رفتار pop، و اینکه آیا مقصد باید در حالت تک بالا راهاندازی شود یا خیر.
گزینه ها با نوشتن
به طور پیش فرض، navigate() مقصد جدید شما را به پشته اضافه می کند. می توانید رفتار navigate() را با ارسال گزینه های ناوبری اضافی به navigate() خود تغییر دهید.
می توانید با استفاده از یک لامبدا ساده یک نمونه از NavOptions ایجاد کنید. آرگومان هایی را که در غیر این صورت ممکن است به صراحت به NavOptions.Builder ارسال کنید navigate() ارسال کنید. به مثال های زیر توجه کنید:
برای مثال، راهنمای پشته پشته را برای مثالهایی در مورد نحوه ارسال گزینهها برای navigate() در متن ببینید.
گزینه های با XML
در زیر یک نمودار مثالی متشکل از دو صفحه به همراه عملکردی برای پیمایش از یکی به دیگری ارائه شده است:
هنگامی که نمودار ناوبری باد می شود، این اقدامات تجزیه می شوند و اشیاء NavAction مربوطه با پیکربندی های تعریف شده در نمودار تولید می شوند. به عنوان مثال، action_b_to_a به عنوان حرکت از مقصد b به مقصد a تعریف می شود. این اکشن شامل انیمیشنهایی به همراه رفتار popTo است که همه مقصدها را از پشت پشتی حذف میکند. همه این تنظیمات به عنوان NavOptions ثبت می شوند و به NavAction متصل می شوند.
برای دنبال کردن این NavAction ، از NavController.navigate() استفاده کنید و شناسه اکشن را ارسال کنید، همانطور که در مثال زیر نشان داده شده است:
navController.navigate(R.id.action_b_to_a)
گزینه ها را به صورت برنامه ای اعمال کنید
مثال های قبلی نحوه تعیین NavOptions در نمودار ناوبری XML نشان می دهد. با این حال، گزینههای خاص بسته به محدودیتهایی که در زمان ساخت ناشناخته هستند، میتوانند متفاوت باشند. در چنین مواردی، NavOptions باید به صورت برنامه ریزی شده ایجاد و تنظیم شود، همانطور که در مثال زیر نشان داده شده است:
کاتلین
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 به صورت برنامهنویسی اعمال کنید:
کاتلین
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 می گیرد.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]