יעד יכול להיות פעילות בתרשים הניווט. אומנם מומלץ לנהל פעילות אחת באפליקציה, אפליקציות בדרך כלל משתמשות פעילויות שונות לרכיבים נפרדים או למסכים נפרדים באפליקציה. פעילות יכולים להיות שימושיים במקרים כאלה.
כתיבה ו-Kotlin DSL
הוספת יעד פעילות לתרשים הניווט היא בעצם זהה
גם ב-Compose וגם כשמשתמשים ב-DSL של Kotlin עם מקטעים. הסיבה לכך היא
כשמעבירים את NavGraph
לתוכן הקומפוזבילי NavHost
, משתמשים
createGraph()
למבדה.
למידע נוסף, ראו יצירת תרשים באופן פרוגרמטי באמצעות Kotlin DSL.
XML
יצירה של יעד פעילות דומה ליצירת מקטע היעד. עם זאת, האופי של יעד פעילות הוא אחרת.
כברירת מחדל, ספריית הניווט מצרפת את NavController
אל
הפריסה Activity
, ותרשים הניווט הפעיל בהיקף של
Activity
. אם משתמש מנווט אל Activity
אחר, הערך הנוכחי
תרשים הניווט כבר לא נכלל בקטגוריה הזו. כלומר, Activity
צריך להיחשב כנקודת קצה בתרשים ניווט.
כדי להוסיף יעד פעילות, צריך לציין את היעד Activity
עם היעד שלו
שם מחלקה מלא:
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigation_graph" app:startDestination="@id/simpleFragment"> <activity android:id="@+id/sampleActivityDestination" android:name="com.example.android.navigation.activity.DestinationActivity" android:label="@string/sampleActivityTitle" /> </navigation>
קוד ה-XML הזה מקביל לקריאה הבאה ל-startActivity()
:
Kotlin
startActivity(Intent(context, DestinationActivity::class.java))
Java
startActivity(new Intent(context, DestinationActivity.class));
יכולים להיות מקרים שבהם הגישה הזו לא מתאימה. לדוגמה,
שאין להם תלות בזמן הידור (compile-time) במחלקה ברמת הפעילות, או שעשויה להיות
להעדיף את רמת העקיפה של כוונה מרומזת.
הערך intent-filter
ברשומת המניפסט של היעד Activity
קובעת את המבנה של היעד Activity
.
לדוגמה, שימו לב לקובץ המניפסט הבא:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.navigation.activity">
<application>
<activity android:name=".DestinationActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data
android:host="example.com"
android:scheme="https" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
צריך להגדיר את היעד התואם של Activity
עם
המאפיינים action
ו-data
שתואמים למאפיינים ברשומת המניפסט:
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigation_graph" app:startDestination="@id/simpleFragment"> <activity android:id="@+id/localDestinationActivity" android:label="@string/localActivityTitle" app:action="android.intent.action.VIEW" app:data="https://example.com" app:targetPackage="${applicationId}" /> </navigation>
ציון הערך targetPackage
למכשיר applicationId
הנוכחי מגביל
היקף לאפליקציה הנוכחית, שכולל את האפליקציה הראשית.
אפשר להשתמש באותו מנגנון במקרים שבהם רוצים שאפליקציה ספציפית
היעד. בדוגמה הבאה יעד מוגדר כאפליקציה עם
applicationId
מתוך com.example.android.another.app
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigation_graph" app:startDestination="@id/simpleFragment"> <activity android:id="@+id/localDestinationActivity" android:label="@string/localActivityTitle" app:action="android.intent.action.VIEW" app:data="https://example.com" app:targetPackage="com.example.android.another.app" /> </navigation>
ארגומנטים דינמיים
בדוגמאות הקודמות השתמשו בכתובות URL קבועות כדי לנווט ליעדים. ייתכן ש
לתמוך בכתובות URL דינמיות שאליהן נשלח מידע נוסף
כתובת URL. לדוגמה, אפשר לשלוח מזהה משתמש בכתובת URL בפורמט שדומה לזה:
https://example.com?userId=<actual user ID>
במקרה הזה, במקום המאפיין data
, משתמשים ב-dataPattern
.
לאחר מכן אפשר לציין ארגומנטים שיוחלפו ב-placeholders בעלי שם
הערך dataPattern
:
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigation_graph" app:startDestination="@id/simpleFragment"> <activity android:id="@+id/localDestinationActivity" android:label="@string/localActivityTitle" app:action="android.intent.action.VIEW" app:dataPattern="https://example.com?userId={userId}" app:targetPackage="com.example.android.another.app"> <argument android:name="userId" app:argType="string" /> </activity> </navigation>
בדוגמה הזו אפשר לציין את הערך של userId
באמצעות אחת מהאפשרויות Safe Args
או באמצעות Bundle
:
Kotlin
navController.navigate(
R.id.localDestinationActivity,
bundleOf("userId" to "someUser")
)
Java
Bundle args = new Bundle();
args.putString("userId", "someUser");
navController.navigate(R.id.localDestinationActivity, args);
דוגמה זו מחליפה את someUser
ב-{userId}
ויוצרת ערך URI של
https://example.com?userId=someUser
.