تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يعمل مكوِّن التنقل مع نظام التشغيل Android للحفاظ على
حزمة الرجوع بصفتك المستخدم
في تطبيقك. وفي بعض الحالات، قد يكون من المفيد الحفاظ على عمليات
المكدسات الخلفية في نفس الوقت، مع تحرك المستخدم ذهابًا وإيابًا بينها.
على سبيل المثال، إذا كان تطبيقك يتضمّن الجزء السفلي من
التنقل أو التنقل
درج، رجوع متعدد
دعم المكدس، الذي يسمح للمستخدمين بالتبديل بين المسارات في التطبيق بحرية
بدون أن يفقدوا مكانهم في أي منها.
يوفر مكوِّن التنقل واجهات برمجة تطبيقات تدعم عدة حزم خلفية عن طريق
حفظ حالة الوجهات واستعادتها في صفحة التنقل
الرسم البياني. تشير رسالة الأشكال البيانية
تتضمن الصفة NavigationUI
تعالج هذا الأمر تلقائيًا، بل يمكنك أيضًا استخدام واجهات برمجة التطبيقات الأساسية
يدويًا لإجراء عملية تنفيذ أكثر تخصيصًا.
التنفيذ التلقائي باستخدام ميزة NavigationUI
الصف NavigationUI
تضم واجهات برمجة تطبيقات تعمل تلقائيًا على حفظ حالة عناصر القائمة واستعادتها
المستخدم بينهم. وتنفِّذ واجهات برمجة التطبيقات هذه دعمًا متعددًا لتسلسل استدعاء الدوال البرمجية من خلال
تلقائيًا في الحالات التالية:
لا تتطلّب واجهات برمجة التطبيقات هذه أي تغييرات إضافية على الرمز لتنفيذ تسلسل استدعاء الدوال البرمجية المتعددة.
وهي الطريقة الموصى بها لدعم حزم خلفية متعددة في
التطبيق.
تنفيذ الدعم يدويًا باستخدام واجهات برمجة التطبيقات الأساسية
في حال عدم استيفاء العناصر المقدَّمة من "NavigationUI" لمتطلباتك، يمكنك
استخدام واجهات برمجة التطبيقات الأساسية لحفظ حزم البيانات السابقة واستعادتها من خلال أحد
أسطح واجهة برمجة التطبيقات الأخرى التي يوفرها مكوِّن التنقل.
تنسيق XML للتنقّل
في ملف XML الخاص بالتنقل، يمكن لعناصر <action> في الرسم البياني للتنقل استخدام
app:popUpToSaveState لحفظ حالة أي وجهات
تم فرقعة الإجراء باستخدام app:popUpTo. يمكنه أيضًا استخدام واجهة برمجة التطبيقات app:restoreState.
لاستعادة أي حالة تم حفظها سابقًا للوجهة المحددة في
السمة app:destination.
يمكنك استخدام هذه السمات لإنشاء عدة حزم خلفية. عند إجراء تنقل
إجراء يحتاج إلى نقل المستخدم من حزمة خلفية إلى أخرى، وتعيين كليهما
app:popUpToSaveState وapp:restoreState إلى true في
<action>. بهذه الطريقة، يحفظ الإجراء حالة الرجوع الحالي
مكدس البيانات مع استعادة الحالة التي تم حفظها سابقًا للوجهة مرة أخرى
المكدس، إذا كان موجودًا.
الصف NavOptions
يتيح لك أيضًا تمرير خيارات تنقّل خاصة لحفظ حِزم الرجوع إلى أخرى واستعادتها.
عند التنقّل باستخدام NavController. هذا صحيح سواء قمت بإنشاء
مثال لـ NavOptions باستخدام لغة Kotlin
DSL أو باستخدام
NavOptions.Builder:
Kotlin
// Use the navigate() method that takes a navOptions DSL BuildernavController.navigate(selectedBottomNavRoute){launchSingleTop=truerestoreState=truepopUpTo(navController.graph.findStartDestination().id){saveState=true}}
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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,["# Support multiple back stacks\n\nThe Navigation component works with the Android operating system to maintain the\n[back stack](/guide/navigation/navigation-navigate#back-stack) as the user\nnavigates in your app. In some cases, it might be helpful to maintain *multiple\nback stacks* at the same time, with the user moving back and forth between them.\nFor example, if your app includes [bottom\nnavigation](/guide/navigation/navigation-ui#bottom_navigation) or a [navigation\ndrawer](/guide/navigation/navigation-ui#add_a_navigation_drawer), multiple back\nstack support allows your users to switch freely between flows in your app\nwithout losing their place in any of them.\n\nThe Navigation component provides APIs that support multiple back stacks by\nsaving and restoring the state of destinations in your [navigation\ngraph](/guide/navigation/navigation-design-graph). The\n[`NavigationUI`](/reference/androidx/navigation/ui/NavigationUI) class includes\nmethods that handle this automatically, but you can also use the underlying APIs\nmanually for a more customized implementation.\n| **Note:** The Navigation component only provides multiple back stack support in version 2.4.0 and higher.\n\nImplement support automatically with NavigationUI\n-------------------------------------------------\n\nThe [`NavigationUI`](/reference/androidx/navigation/ui/NavigationUI) class\nincludes APIs that automatically save and restore the state of menu items as the\nuser moves between them. These APIs implement multiple back stack support by\ndefault in the following cases:\n\n- When you use the appropriate overload of [`setupWithNavController()`](/reference/androidx/navigation/ui/NavigationUI#setupWithNavController(com.google.android.material.navigation.NavigationView,androidx.navigation.NavController)) to associate an instance of `NavigationView` or `BottomNavigationView` with a [`NavController`](/reference/androidx/navigation/NavController) instance, as described in [Add a navigation\n drawer](/guide/navigation/navigation-ui#add_a_navigation_drawer) or [Bottom\n navigation](/guide/navigation/navigation-ui#bottom_navigation).\n- When you use [`onNavDestinationSelected()`](/reference/androidx/navigation/ui/NavigationUI#onNavDestinationSelected(android.view.MenuItem,androidx.navigation.NavController)) to create a [custom navigation menu\n UI](/guide/navigation/navigation-ui#Tie-navdrawer) tied to destinations hosted by a `NavController` instance.\n\nThese APIs require no further code changes to implement multiple back stack\nsupport, and are the recommended way of supporting multiple back stacks in your\napp.\n\nImplement support manually with underlying APIs\n-----------------------------------------------\n\nIf the elements provided by `NavigationUI` don't satisfy your requirements, you\ncan use the underlying APIs for saving and restoring back stacks through one of\nthe other API surfaces provided by the Navigation component.\n\n### Navigation XML\n\nIn Navigation XML, `\u003caction\u003e` elements in your navigation graph can use the\n`app:popUpToSaveState` attribute to save the state of any destinations that the\naction popped using `app:popUpTo`. They can also use the `app:restoreState`\nattribute to restore any previously saved state for the destination defined in\nthe `app:destination` attribute.\n\nYou can use these attributes to support multiple back stacks. When a navigation\naction needs to move the user from one back stack to another, set both\n`app:popUpToSaveState` and `app:restoreState` to `true` in the corresponding\n`\u003caction\u003e` element. That way, the action saves the state of the current back\nstack while also restoring the previously saved state of the destination back\nstack, if it exists.\n\nThe following example shows an action that uses both of these attributes: \n\n \u003caction\n android:id=\"@+id/swap_stack\"\n app:destination=\"@id/second_stack\"\n app:restoreState=\"true\"\n app:popUpTo=\"@id/first_stack_start_destination\"\n app:popUpToSaveState=\"true\" /\u003e\n\n### NavOptions\n\nThe [`NavOptions`](/reference/kotlin/androidx/navigation/NavOptions) class\nallows you to pass special navigation options to save and restore back stacks\nwhen you navigate using a `NavController`. This is true whether you create your\ninstance of `NavOptions` using the [Kotlin\nDSL](/guide/navigation/navigation-kotlin-dsl) or using the\n[`NavOptions.Builder`](/reference/kotlin/androidx/navigation/NavOptions.Builder): \n\n### Kotlin\n\n```kotlin\n// Use the navigate() method that takes a navOptions DSL Builder\nnavController.navigate(selectedBottomNavRoute) {\n launchSingleTop = true\n restoreState = true\n popUpTo(navController.graph.findStartDestination().id) {\n saveState = true\n }\n}\n```\n\n### Java\n\n```java\nNavOptions navOptions = new NavOptions.Builder()\n .setLaunchSingleTop(true)\n .setRestoreState(true)\n .setPopUpTo(NavGraph.findStartDestination(navController.getGraph()).getId(),\n false, // inclusive\n true) // saveState\n .build();\nnavController.navigate(selectedBottomNavId, null, navOptions);\n```\n\nTo learn more about passing navigation options, see [Apply NavOptions\nprogrammatically](/guide/navigation/navigation-navigate#programmatic).\n\nAdditional resources\n--------------------\n\nTo learn more about multiple back stack support with the Navigation component,\nsee the following additional resources:\n\n### Blog posts\n\n- [MAD Skills: Navigation multiple back stacks](https://medium.com/androiddevelopers/navigation-multiple-back-stacks-6c67ba41952f) on Medium\n- [Navigation: Multiple Back Stacks deep dive](https://medium.com/androiddevelopers/multiple-back-stacks-b714d974f134) on Medium\n\n### Samples\n\n- [Now in Android\n app](https://github.com/android/nowinandroid/blob/main/app/src/main/java/com/google/samples/apps/nowinandroid/navigation/NiaTopLevelNavigation.kt#L38) on GitHub\n- [Jetnews](https://github.com/android/compose-samples/blob/main/JetNews/app/src/main/java/com/example/jetnews/ui/JetnewsNavigation.kt#L30) on GitHub\n- [Jetsnack](https://github.com/android/compose-samples/blob/main/Jetsnack/app/src/main/java/com/example/jetsnack/ui/JetsnackAppState.kt#L119) on GitHub"]]