הוספת תמיכה בסוגי יעדים חדשים
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
הסוג NavController
מסתמך על אובייקט Navigator
אחד או יותר כדי לבצע את פעולת הניווט. כברירת מחדל, NavController
תומך ביציאה מתרשים הניווט על ידי ניווט לפעילות אחרת באמצעות הכיתה ActivityNavigator
והכיתה המשולבת ActivityNavigator.Destination
.
כדי לנווט ליעד מסוג אחר, צריך להוסיף ל-NavController
עוד אובייקט Navigator
אחד או יותר. לדוגמה, כשמשתמשים בקטעי קוד כיעדים, ה-NavHostFragment
מוסיף באופן אוטומטי את הכיתה FragmentNavigator
ל-NavController
שלו.
כדי להוסיף אובייקט Navigator
חדש ל-NavController
, משתמשים ב-method getNavigatorProvider()
ואז ב-method 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).
[null,null,["עדכון אחרון: 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)"]]