可使用現有的片段或活動建立目的地。您也可以使用導覽編輯器來建立新的目的地,或是建立預留位置,以便日後替換成片段或活動。
從現有片段或活動建立目的地
在導覽編輯器中,如果有想要新增至導覽圖表的現有目的地類型,請按一下「新增目的地」,然後在顯示的下拉式選單中,按一下對應的目的地。您現在可以在「Design」(設計) 檢視畫面中查看目的地預覽畫面,導覽圖表的「Text」(文字) 檢視畫面中也會顯示對應 XML。
建立新的片段目的地
若要使用導覽編輯器新增目的地類型,請執行下列操作:
- 在導覽編輯器中,按一下「新增目的地」圖示
,然後按一下「Create new destination」(建立新的目的地)。
- 在隨即顯示的「New Android Component」(新增 Android 元件) 對話方塊中建立您的片段。若要進一步瞭解片段,請參閱片段說明文件。
返回導覽編輯器,請注意 Android Studio 已將這個目的地新增至圖表。
圖 1 是目的地和預留位置目的地的範例。

從 DialogFragment 建立目的地
如果您有現有的 DialogFragment
,可使用 <dialog>
元素將對話方塊新增至導覽圖表,如以下範例所示:
<?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/nav_graph"> ... <dialog android:id="@+id/my_dialog_fragment" android:name="androidx.navigation.myapp.MyDialogFragment"> <argument android:name="myarg" android:defaultValue="@null" /> <action android:id="@+id/myaction" app:destination="@+id/another_destination"/> </dialog> ... </navigation>
建立新的活動目的地
建立 Activity
目的地與建立 Fragment
目的地類似。不過,Activity
目的地的性質其實大不相同。
根據預設,導覽程式庫會將 NavController
附加至 Activity
版面配置,且使用中的導覽圖表範圍僅限於使用中的 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));
在某些情況下,這個做法會不適用。例如,活動類別可能沒有編譯時間依附元件,或者您可能偏好使用隱含意圖的間接層級。目的地 Activity
資訊清單輸入項中的 intent-filter
決定您必須如何建立 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>
動態引數
先前的範例使用固定網址前往目的地。您也可能需要支援動態網址,其以網址的一部分傳送額外資訊。例如,您可在格式與 https://example.com?userId=<actual user ID>
相似的網址中傳送使用者 ID。
在此情況下,請使用 dataPattern
,而非 data
屬性。接著,您可以在 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>
在此範例中,您可以使用 Safe Args 或 Bundle
指定 userId
值:
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}
,並建立 https://example.com?userId=someUser
的 URI 值。
預留位置目的地
您可以使用「預留位置」來表示未實作的目的地。預留位置是做為目的地的視覺表示。在導覽編輯器中,您可像使用其他目的地一樣使用預留位置。