[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Create a navigation controller\n\nThe navigation controller is one of the [key concepts](/guide/navigation#types) in navigation. It\nholds the navigation graph and exposes methods that allow your app to move\nbetween the destinations in the graph.\n\nWhen using the [Navigation component](/reference/androidx/navigation/package-summary), you create a navigation controller\nusing the [`NavController`](/reference/androidx/navigation/NavController) class. [`NavController`](/reference/androidx/navigation/NavController) is the central\nnavigation API. It tracks which destinations the user has visited, and allows\nthe user to move between destinations. This guide demonstrates how to create a\n`NavController` in your app.\n\nFor information on how to add a navigation graph to your `NavController`, see\n[Design your navigation graph](/guide/navigation/design). `NavController` provides a few different ways\nto navigate to the destinations in its graph. For more, see [Navigate to a\ndestination](/guide/navigation/use-graph/navigate).\n| **Note:** Each `NavHost` you create has its own corresponding `NavController`. The `NavController` provides access to the `NavHost`'s graph.\n\nCompose\n-------\n\nTo create a `NavController` when using Jetpack Compose, call\n[`rememberNavController()`](/reference/kotlin/androidx/navigation/compose/package-summary#rememberNavController(kotlin.Array)): \n\n val navController = rememberNavController()\n\nYou should create the `NavController` high in your composable hierarchy. It\nneeds to be high enough that all the composables that need to reference it can\ndo so.\n\nDoing so lets you to use the `NavController` as the single source of truth for\nupdating composables outside of your screens. This follows the principles of\n[state hoisting](/jetpack/compose/state#state-hoisting).\n\nViews\n-----\n\nIf you are using the Views UI framework, you can retrieve your NavController\nusing one of the following methods depending on the context:\n\n**Kotlin:**\n\n- [`Fragment.findNavController()`](/reference/kotlin/androidx/navigation/fragment/package-summary#(androidx.fragment.app.Fragment).findNavController())\n- [`View.findNavController()`](/reference/kotlin/androidx/navigation/package-summary#%28android.view.View%29.findNavController%28%29)\n- [`Activity.findNavController(viewId: Int)`](/reference/kotlin/androidx/navigation/package-summary#(android.app.Activity).findNavController(kotlin.Int))\n\n**Java:**\n\n- [`NavHostFragment.findNavController(Fragment)`](/reference/androidx/navigation/fragment/NavHostFragment#findNavController(androidx.fragment.app.Fragment))\n- [`Navigation.findNavController(Activity, @IdRes int viewId)`](/reference/androidx/navigation/Navigation#findNavController(android.app.Activity,%20int))\n- [`Navigation.findNavController(View)`](/reference/androidx/navigation/Navigation#findNavController(android.view.View))\n\nTypically, you first get a `NavHostFragment`, and then retrieve the\n`NavController` from the fragment. The following snippet demonstrates this: \n\n### Kotlin\n\n val navHostFragment =\n supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment\n val navController = navHostFragment.navController\n\n### Java\n\n NavHostFragment navHostFragment =\n (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);\n NavController navController = navHostFragment.getNavController();\n\n| **Warning:** You can encounter problems when creating the `NavHostFragment` using `FragmentContainerView` or when manually adding the `NavHostFragment` to your activity using a `FragmentTransaction`. If you do so, you can cause `Navigation.findNavController(Activity, @IdRes int)` to fail if you attempt to retrieve the `NavController` in `onCreate()`. You should retrieve the `NavController` directly from the `NavHostFragment` instead, as in the preceding example.\n\nFurther reading\n---------------\n\n- **[Design your navigation graph](/guide/navigation/design):** A guide detailing how to add a graph to your `NavController` that contains all the destinations in your app.\n- **[Navigate to a destination](/guide/navigation/use-graph/navigate):** A guide detailing how to use the `NavController` to move between destinations in your navigation graph."]]