Stay organized with collections
Save and categorize content based on your preferences.
The navigation controller is one of the key concepts in navigation. It
holds the navigation graph and exposes methods that allow your app to move
between the destinations in the graph.
When using the Navigation component, you create a navigation controller
using the NavController class. NavController is the central
navigation API. It tracks which destinations the user has visited, and allows
the user to move between destinations. This guide demonstrates how to create a
NavController in your app.
For information on how to add a navigation graph to your NavController, see
Design your navigation graph. NavController provides a few different ways
to navigate to the destinations in its graph. For more, see Navigate to a
destination.
You should create the NavController high in your composable hierarchy. It
needs to be high enough that all the composables that need to reference it can
do so.
Doing so lets you to use the NavController as the single source of truth for
updating composables outside of your screens. This follows the principles of
state hoisting.
Views
If you are using the Views UI framework, you can retrieve your NavController
using one of the following methods depending on the context:
Design your navigation graph: A guide detailing how to add a graph
to your NavController that contains all the destinations in your app.
Navigate to a destination: A guide detailing how to use the
NavController to move between destinations in your navigation graph.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],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."]]