สถานที่ทำกิจกรรม

ปลายทางอาจเป็นกิจกรรมก็ได้ในกราฟการนำทาง แม้ว่าดีที่สุด ฝึกให้มีกิจกรรมเดียวในแอป แอปต่างๆ มักใช้ กิจกรรมสำหรับคอมโพเนนต์หรือหน้าจอที่แตกต่างกันภายในแอป กิจกรรม ปลายทางก็อาจมีประโยชน์ในกรณีเช่นนี้

Compose และ Kotlin DSL

โดยพื้นฐานแล้ว การเพิ่มจุดหมายของกิจกรรมลงในกราฟการนำทางนั้นเหมือนกัน ทั้งใน Compose และเมื่อใช้ Kotlin DSL กับ Fragment นั่นเป็นเพราะ เมื่อส่ง NavGraph ไปยัง Composable ของ 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));

คุณอาจมีกรณีที่วิธีการนี้ไม่เหมาะสม ตัวอย่างเช่น คุณสามารถ อาจไม่มีทรัพยากร Dependency ของเวลาคอมไพล์บนคลาสกิจกรรม หรือคุณอาจ ต้องการระดับของทางอ้อมที่จะผ่านความตั้งใจโดยนัย intent-filter ในรายการไฟล์ Manifest ของปลายทาง Activity จะกำหนดวิธีจัดโครงสร้างปลายทาง Activity

ตัวอย่างเช่น ลองพิจารณาไฟล์ Manifest ต่อไปนี้

<?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 ที่ตรงกับแอตทริบิวต์ในรายการไฟล์ Manifest

<?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 เช่น คุณอาจส่ง User-ID ใน URL ที่มีรูปแบบคล้ายกับ https://example.com?userId=<actual user 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>

ในตัวอย่างนี้ คุณสามารถระบุค่า 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