Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bộ điều khiển hoạt động điều hướng là một trong những khái niệm chính trong tính năng điều hướng. Nó giữ biểu đồ điều hướng và hiển thị các phương thức cho phép ứng dụng của bạn di chuyển giữa các đích đến trong biểu đồ.
Khi sử dụng thành phần Điều hướng, bạn sẽ tạo một bộ điều khiển hoạt động điều hướng bằng cách sử dụng lớp NavController. NavController là API điều hướng trung tâm. API này theo dõi các đích đến mà người dùng đã truy cập và cho phép người dùng di chuyển giữa các đích đến. Hướng dẫn này minh hoạ cách tạo NavController trong ứng dụng của bạn.
Để biết thông tin về cách thêm một biểu đồ điều hướng vào NavController, hãy xem bài viết Thiết kế biểu đồ điều hướng. NavController cung cấp một vài cách để điều hướng đến các đích đến trong biểu đồ của nó. Để biết thêm thông tin, hãy xem bài viết Di chuyển tới một đích đến.
Bạn nên tạo NavController ở vị trí cao trong hệ phân cấp thành phần kết hợp. Lớp này cần phải ở vị trí đủ cao để tất cả thành phần kết hợp cần tham chiếu đến lớp này đều có thể làm như vậy.
Khi làm vậy, bạn sẽ có thể dùng NavController làm nguồn đáng tin cậy duy nhất để cập nhật các thành phần kết hợp bên ngoài màn hình. Điều này tuân theo các nguyên tắc chuyển trạng thái lên trên (state hoisting).
Khung hiển thị
Nếu đang dùng khung giao diện người dùng của Khung hiển thị, bạn có thể truy xuất NavController bằng một trong nhiều phương thức, tuỳ thuộc vào ngữ cảnh.
Thiết kế biểu đồ điều hướng: Hướng dẫn nêu chi tiết cách thêm một biểu đồ vào NavController chứa tất cả đích đến trong ứng dụng của bạn.
Điều hướng tới một đích đến: Hướng dẫn nêu chi tiết cách sử dụng NavController để di chuyển giữa các đích đến trong biểu đồ điều hướng.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 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."]]