// Use the navigate() method that takes a navOptions DSL BuildernavController.navigate(selectedBottomNavRoute){launchSingleTop=truerestoreState=truepopUpTo(navController.graph.findStartDestination().id){saveState=true}}
[null,null,["最后更新时间 (UTC):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"]]