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

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

Compose และ Kotlin DSL

การเพิ่มปลายทางกิจกรรมลงในกราฟการนําทางจะเหมือนกันทั้งใน Compose และเมื่อใช้ Kotlin DSL กับ Fragment เนื่องจากเมื่อส่ง NavGraph ไปยัง Composable NavHost คุณจะใช้ Lambda createGraph() เดียวกัน

ดูข้อมูลเพิ่มเติมได้ที่ส่วนย่อยและ Kotlin DSL

XML

การสร้างปลายทางกิจกรรมคล้ายกับการสร้างปลายทางส่วนย่อย อย่างไรก็ตาม ลักษณะของปลายทางกิจกรรมค่อนข้างแตกต่างกัน

โดยค่าเริ่มต้น Navigation library จะแนบ NavController กับเลย์เอาต์ Activity และกราฟการนำทางที่ใช้งานอยู่จะกำหนดขอบเขตเป็น Activity ที่ใช้งานอยู่ หากผู้ใช้ไปยัง Activity อื่น กราฟการนำทางปัจจุบันจะไม่อยู่ในขอบเขตอีกต่อไป ซึ่งหมายความว่าปลายทาง Activity ควรได้รับการพิจารณาว่าเป็นปลายทางภายในกราฟการนำทาง

หากต้องการเพิ่มปลายทางกิจกรรม ให้ระบุปลายทาง Activity พร้อมชื่อคลาสที่สมบูรณ์ในตัวเอง ดังนี้

<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() ต่อไปนี้

startActivity(Intent(context, DestinationActivity::class.java))

คุณอาจมีกรณีที่แนวทางนี้ไม่เหมาะสม เช่น คุณอาจไม่มีทรัพยากร Dependency ของเวลาคอมไพล์ในคลาสกิจกรรม หรือคุณอาจต้องการระดับการอ้อมผ่าน Intent โดยนัย 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 ดังนี้

<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

<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>

ในกรณีนี้ ให้ใช้ dataPattern แทนแอตทริบิวต์ data จากนั้นคุณสามารถระบุอาร์กิวเมนต์ที่จะใช้แทนตัวยึดตำแหน่งที่มีชื่อภายในค่า dataPattern ได้ดังนี้

<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 ดังนี้

navController.navigate(
    R.id.localDestinationActivity,
    Bundle().apply { putString("userId", "someUser") }
)

ตัวอย่างนี้จะแทนที่ someUser ด้วย {userId} และสร้างค่า URI เป็น https://example.com?userId=someUser