대상으로 돌아가야 하는 위치의 명확한 예는 탐색이 순환인 경우입니다. 이 문서에서는 해당 사용 사례를 간략하게 설명합니다.
시나리오
앱에 A, B, C라는 세 가지 대상이 있다고 가정해 보겠습니다. 또한 A에서 B로, B에서 C로, C에서 A로 되돌아가는 작업이 있습니다. 이에 상응하는 탐색 그래프는 다음과 같습니다.
그림 1. A, B, C 세 개의 대상이 있는 순환 탐색 그래프
각 탐색 작업에서 NavController는 새 대상을 백 스택에 추가합니다. 따라서 다이어그램의 흐름을 반복적으로 탐색하면 백 스택에 각 대상의 여러 집합(A, B, C, A, B, C, A, B, C)이 포함됩니다.
해결 방법
백 스택에서 반복되지 않도록 하려면 NavController.navigate() 호출에서 또는 탐색 작업에서 popUpTo() 및 inclusive를 지정합니다.
대상 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}}},)}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 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,["# 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"]]