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ạn có thể dùng thao tác chung để tạo một thao tác phổ biến mà nhiều đích đến có thể sử dụng. Ví dụ: bạn có thể muốn các nút ở nhiều đích đến điều hướng tới cùng một màn hình chính của ứng dụng.
Thao tác chung được biểu thị trong Trình chỉnh sửa điều hướng bằng một mũi tên nhỏ trỏ tới đích đến được liên kết, như minh hoạ trong hình 1.
Hình 1. Thao tác chung cho ra đồ hoạ lồng nhau.
Tạo thao tác chung
Để tạo thao tác chung, hãy làm như sau:
Từ Graph Editor (Trình chỉnh sửa Đồ hoạ), nhấp vào một đích đến để làm nổi bật đích đó.
Nhấp chuột phải vào đích đến để hiển thị trình đơn theo bối cảnh.
Chọn Add Action > Global (Thêm Thao tác > Chung). Một mũi tên () xuất hiện ở bên trái đích đến.
Nhấp vào thẻ Text (Văn bản) để điều hướng đến khung hiển thị văn bản XML. XML cho thao tác chung có dạng như sau:
Để sử dụng thao tác chung trong mã của bạn, hãy truyền mã nhận dạng tài nguyên của thao tác chung đến phương thức navigate() cho từng phần tử trên giao diện người dùng, như minh hoạ trong ví dụ sau:
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,["# Global actions\n\nYou can use a *global action* to create a common action that multiple\ndestinations can use. For example, you might want buttons in\ndifferent destinations to navigate to the same main app screen.\n\nA global action is represented in the Navigation Editor by a small arrow that\npoints to the associated destination, as shown in figure 1.\n**Figure 1.** A global action that leads to a nested graph.\n\nCreate a global action\n----------------------\n\nTo create a global action, do the following:\n\n1. From the **Graph Editor**, click on a destination to highlight it.\n2. Right-click on the destination to display the context menu.\n3. Select **Add Action \\\u003e Global** . An arrow () appears to the left of the destination.\n4. Click the **Text** tab to navigate to the XML text view. The XML for the\n global action looks similar to the following:\n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cnavigation xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n xmlns:tools=\"http://schemas.android.com/tools\"\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:id=\"@+id/main_nav\"\n app:startDestination=\"@id/mainFragment\"\u003e\n\n ...\n\n \u003caction android:id=\"@+id/action_global_mainFragment\"\n app:destination=\"@id/mainFragment\"/\u003e\n\n \u003c/navigation\u003e\n\nUse a global action\n-------------------\n\nTo use a global action in your code, pass the resource ID of the global action\nto the [`navigate()`](/reference/androidx/navigation/NavController#navigate(int))\nmethod for each UI element, as shown in the following example: \n\n### Kotlin\n\n```kotlin\nviewTransactionButton.setOnClickListener { view -\u003e\n view.findNavController().navigate(R.id.action_global_mainFragment)\n}\n```\n\n### Java\n\n```java\nviewTransactionsButton.setOnClickListener(new View.OnClickListener() {\n @Override\n public void onClick(View view) {\n Navigation.findNavController(view).navigate(R.id.action_global_mainFragment);\n }\n});\n```\n\nUse Safe Args with a global action\n----------------------------------\n\nFor information on using Safe Args with global actions, see\n[Pass data between destinations](/topic/libraries/architecture/navigation/navigation-pass-data#safe-args-global).\n\nAdditional resources\n--------------------\n\nTo learn more about navigation, consult the following\nadditional resources.\n\n### Codelabs\n\n- [Navigation Codelab](https://codelabs.developers.google.com/codelabs/android-navigation/index.html?index=..%2F..%2Findex#0)\n\n### Videos\n\n- [Android Jetpack:\n manage UI navigation with Navigation Controller (Google I/O '18)](https://www.youtube.com/watch?v=8GCXtCjtg40)"]]