تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
وخير مثال على المكان الذي تحتاج فيه إلى العودة إلى وجهة ما هو عندما
التنقل دائري. يوضّح هذا المستند حالة الاستخدام هذه.
السيناريو
لنفترض أنّ تطبيقك يتضمّن ثلاث وجهات: "أ" و"ب" و"ج". كما أن لديه إجراءات
العميل من A إلى B، ومن B إلى C، ومن C إلى A. الرسم البياني للتنقل المقابل
تظهر على النحو التالي:
الشكل 1. رسم بياني دائري للتنقّل تشمل ثلاث وجهات: "أ" و"ب" و"ج".
مع كل إجراء تنقُّل، تُضيف NavController الوجهة الجديدة إلى
حزمة الرجوع. وعلى هذا النحو، فإن التنقل المتكرر عبر التدفق في الرسم التخطيطي
قد تتسبب في احتواء الحزمة الخلفية على مجموعات متعددة لكل وجهة: A، B،
ج، أ، ب، ج، أ، ب، ج.
الحل
لتجنّب التكرار في حزمة الخلفية، حدِّد popUpTo() و
inclusive في مكالمتك مع NavController.navigate() أو في
إجراء التنقل.
ضع في اعتبارك الحالة التي تحتوي فيها حزمة الخلفية بعد الوصول إلى الوجهة "ج" على واحد
مثيل لكل وجهة: أ، ب، ج. ينبغي أن تتأكد من أنك حددت
popUpTo() وinclusive في الإجراء أو عبارة الحث على navigate() التي تتخذ
المستخدم من الوجهة "ج" إلى الوجهة "أ".
في هذه الحالة، عندما ينتقل المستخدم من الوجهة C إلى الوجهة A،
NavController أيضًا ينبثق أيضًا إلى A. هذا يعني أنه يزيل B وC من
المكدس. باستخدام inclusive = true، يؤدي ذلك أيضًا إلى ظهور أول حرف A بشكل فعّال
ومحو المكدس.
إنشاء التنفيذ
في ما يلي طريقة تنفيذ حل popUpTo() الدائري في
الإنشاء:
// When creating your `NavGraph` in your `NavHost`.composable("c"){DestinationC(onNavigateToA={navController.navigate("a"){popUpTo("a"){inclusive=true}}},)}
تنفيذ المشاهدات
في ما يلي طريقة تنفيذ حل popUpTo الدائري في
عدد المشاهدات:
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ 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,["# Circular navigation\n\nA clear example of where you need to pop back to a destination is when your\nnavigation is circular. This document outlines that use case.\n\nScenario\n--------\n\nImagine your app has three destinations: A, B, and C. It also has actions that\nlead from A to B, B to C, and C back to A. The corresponding navigation graph\nappears as follows:\n**Figure 1.** A circular navigation graph with three destinations: A, B, and C.\n\nWith each navigation action, the `NavController` adds the new destination to the\nback stack. As such, repeatedly navigating through the flow in the diagram would\ncause your back stack would to contain multiple sets of each destination: A, B,\nC, A, B, C, A, B, C.\n\nSolution\n--------\n\nTo avoid repetition in your back stack, specify [`popUpTo()`](/guide/navigation/backstack#pop) and\n[`inclusive`](/guide/navigation/backstack#pop-back-destination) in your call to `NavController.navigate()` or in your\nnavigation action.\n\nConsider a case where after reaching destination C, the back stack contains one\ninstance of each destination: A, B, C. You need to ensure that you have defined\n`popUpTo()` and `inclusive` in the action or call to `navigate()` that takes the\nuser from destination C to destination A.\n\nIn this case, when the user navigates from destination C back to destination A,\nthe `NavController` also pops up to A. This means that it removes B and C from\nthe stack. With `inclusive = true`, it also pops the first A, effectively\nclearing the stack.\n| **Note:** This is similar to calling [`popBackStack()` and passing `inclusive`](/guide/navigation/backstack#pop-back-destination).\n\n### Compose implementation\n\nThe following is the implementation of the solution for circular `popUpTo()` in\nCompose: \n\n // When creating your `NavGraph` in your `NavHost`.\n composable(\"c\") {\n DestinationC(\n onNavigateToA = {\n navController.navigate(\"a\") {\n popUpTo(\"a\") {\n inclusive = true\n }\n }\n },\n )\n }\n\n### Views implementation\n\nThe following is the implementation of the solution for circular `popUpTo` in\nViews: \n\n \u003cfragment\n android:id=\"@+id/c\"\n android:name=\"com.example.myapplication.C\"\n android:label=\"fragment_c\"\n tools:layout=\"@layout/fragment_c\"\u003e\n\n \\\u003caction\n android:id=\"@+id/action_c_to_a\"\n app:destination=\"@id/a\"\n app:popUpTo=\"@+id/a\"\n app:popUpToInclusive=\"true\"/\\\u003e\n \u003c/fragment\u003e"]]