탐색 구성요소는 사용자가 앱에서 이동할 때 Android 운영체제와 연동하여 백 스택을 유지합니다. 사용자가 앞뒤로 이동하는 상황에서는 경우에 따라 여러 백 스택을 동시에 유지하는 것이 도움이 될 수 있습니다.
예를 들어, 앱에 하단 탐색이나 탐색 창이 포함된 경우 여러 백 스택 지원을 사용하면 사용자가 앱 흐름 간에 자신의 위치를 잃어버리지 않고 자유롭게 전환할 수 있습니다.
탐색 구성요소는 탐색 그래프에서 대상의 상태를 저장하고 복원하여 여러 백 스택을 지원하는 API를 제공합니다. NavigationUI 클래스에는 이를 자동으로 처리하는 메서드가 포함되지만, 기본 API를 직접 사용하여 더 맞춤설정된 메서드를 구현할 수도 있습니다.
NavigationUI를 사용하여 자동 지원 구현
NavigationUI 클래스에는 사용자가 메뉴 항목 간에 이동할 때 메뉴 항목의 상태를 자동으로 저장하고 복원하는 API가 포함됩니다. 이러한 API는 다음과 같은 경우 기본적으로 여러 백 스택 지원을 구현합니다.
이러한 API는 여러 백 스택을 지원하기 위해 추가적인 코드 변경이 필요하지 않으며 앱에서 여러 백 스택을 지원하는 데 권장하는 방법입니다.
기본 API를 사용하여 직접 구현
NavigationUI에서 제공하는 요소가 요구사항을 충족하지 않는 경우 탐색 구성요소에서 제공하는 다른 API 노출 영역 중 하나를 통해 백 스택을 저장하고 복원하도록 기본 API를 사용할 수 있습니다.
탐색 XML
탐색 XML에서 탐색 그래프의 <action> 요소는 app:popUpToSaveState 속성을 사용하여 작업이 app:popUpTo로 백 스택에서 제거한 대상의 상태를 저장할 수 있습니다. 또한 app:restoreState 속성을 사용하여 app:destination 속성에 정의된 대상의 이전 저장 상태를 복원할 수도 있습니다.
이러한 속성을 사용하여 여러 백 스택을 지원하면 됩니다. 탐색 작업으로 인해 사용자를 하나의 백 스택에서 다른 백 스택으로 옮겨야 하는 경우 대응하는 <action> 요소에 app:popUpToSaveState와 app:restoreState를 true로 설정합니다. 이렇게 하면, 작업이 현재 백 스택의 상태를 저장하는 동시에 이전에 저장된 대상 백 스택의 상태가 있는 경우 상태를 복원합니다.
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}}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 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,["# 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"]]