เพิ่มการรองรับปลายทางประเภทใหม่
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ประเภท NavController
อาศัยออบเจ็กต์ Navigator
อย่างน้อย 1 รายการเพื่อดำเนินการนําทาง โดยค่าเริ่มต้น NavController
รองรับการออกจากกราฟการนําทางโดยไปยังกิจกรรมอื่นโดยใช้คลาส ActivityNavigator
และคลาส ActivityNavigator.Destination
ที่ฝังอยู่
หากต้องการไปยังปลายทางประเภทอื่นๆ คุณต้องเพิ่มNavigator
ออบเจ็กต์อย่างน้อย 1 รายการลงในNavController
ตัวอย่างเช่น เมื่อใช้ข้อมูลโค้ดเป็นปลายทาง NavHostFragment
จะเพิ่มคลาส FragmentNavigator
ลงใน NavController
โดยอัตโนมัติ
หากต้องการเพิ่มออบเจ็กต์ Navigator
ใหม่ลงใน NavController
ให้ใช้เมธอด getNavigatorProvider()
ตามด้วยเมธอด addNavigator()
โค้ดต่อไปนี้แสดงตัวอย่างการเพิ่มออบเจ็กต์ CustomNavigator
ลงใน NavController
Kotlin
val customNavigator = CustomNavigator()
navController.navigatorProvider += customNavigator
Java
CustomNavigator customNavigator = new CustomNavigator();
navController.getNavigatorProvider().addNavigator(customNavigator);
คลาส Navigator
ส่วนใหญ่มีคลาสย่อยปลายทางที่ฝังอยู่ คลาสย่อยนี้ใช้เพื่อระบุแอตทริบิวต์เพิ่มเติมที่เฉพาะเจาะจงสำหรับปลายทางได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับคลาสย่อยปลายทางได้ที่เอกสารอ้างอิงสำหรับคลาส Navigator
ที่เหมาะสม
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับการนําทางได้ที่แหล่งข้อมูลเพิ่มเติมต่อไปนี้
Codelabs
วิดีโอ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Add support for new destination types\n\nThe [`NavController`](/reference/androidx/navigation/NavController) type\nrelies on one or more\n[`Navigator`](/reference/androidx/navigation/Navigator) objects to perform\nthe navigation operation. By default, `NavController` supports leaving the\nnavigation graph by navigating to another activity using the\n[`ActivityNavigator`](/reference/androidx/navigation/ActivityNavigator)\nclass and its nested\n[`ActivityNavigator.Destination`](/reference/androidx/navigation/ActivityNavigator.Destination)\nclass.\n\nTo navigate to any other type of destination, one or more additional `Navigator`\nobjects must be added to the `NavController`. For example, when using fragments\nas destinations, the\n[`NavHostFragment`](/reference/androidx/navigation/fragment/NavHostFragment)\nautomatically adds the\n[`FragmentNavigator`](/reference/androidx/navigation/fragment/FragmentNavigator)\nclass to its `NavController`.\n\nTo add a new `Navigator` object to a `NavController`, use the\n[`getNavigatorProvider()`](/reference/androidx/navigation/NavController#getNavigatorProvider())\nmethod, followed by the\n[`addNavigator()`](/reference/androidx/navigation/NavigatorProvider#addNavigator(androidx.navigation.Navigator))\nmethod.\n\nThe following code shows an example of adding a `CustomNavigator` object to a\n`NavController`: \n\n### Kotlin\n\n```kotlin\nval customNavigator = CustomNavigator()\nnavController.navigatorProvider += customNavigator\n```\n\n### Java\n\n```java\nCustomNavigator customNavigator = new CustomNavigator();\nnavController.getNavigatorProvider().addNavigator(customNavigator);\n```\n\nMost `Navigator` classes have a nested destination subclass. This subclass can\nbe used to specify additional attributes unique to your destination. For more\ninformation about destination subclasses, see the reference documentation for\nthe appropriate [`Navigator`](/reference/androidx/navigation/Navigator)\nclass.\n\nAdditional resources\n--------------------\n\nTo learn more about navigation, see the following\nadditional resources.\n\n\u003cbr /\u003e\n\n### Codelabs\n\n\u003cbr /\u003e\n\n- [Learn Jetpack Navigation codelab](https://codelabs.developers.google.com/codelabs/android-navigation/index.html?index=..%2F..%2Findex#0)\n\n\u003cbr /\u003e\n\n### Videos\n\n\u003cbr /\u003e\n\n- [Android Jetpack: Manage UI navigation with Navigation Controller](https://www.youtube.com/watch?v=8GCXtCjtg40)"]]