假設抵達目的地 C 之後,返回堆疊會包含各一個目的地 A、B、C 的例項。您需要確保已在動作中,或可將使用者從目的地 C 導向目的地 A 的 navigate() 呼叫中,定義 popUpTo() 和 inclusive。
在這種情況下,當使用者從目的地 C 返回目的地 A 時,NavController 也會彈出 A 之後的目的地。也就是說,B 和 C 會從堆疊中移除。運用 inclusive = true,系統也會彈出第一個 A,以便有效清除堆疊。
Compose 實作
以下實作 Compose 中循環 popUpTo() 的解決方案:
// When creating your `NavGraph` in your `NavHost`.composable("c"){DestinationC(onNavigateToA={navController.navigate("a"){popUpTo("a"){inclusive=true}}},)}
[[["容易理解","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"]]