สูตรการสร้าง BackStack สังเคราะห์ของ Deep Link

สูตรนี้แสดงวิธีใช้หลักการนำทางในบริบทของ Deep Link โดยการจัดการ BackStack สังเคราะห์และ Task Stack

โครงสร้างสูตร

สูตรนี้จำลองสถานการณ์ในชีวิตจริงที่ "แอป ก" ทำ Deep Link ไปยัง "แอป ข"

"แอป ก" จำลองโดยโมดูล syntheticbackstack ซึ่ง มี SyntheticBackStackDeepLinkActivity ที่ช่วยให้คุณสร้าง Intent ของ Deep Link และ ทริกเกอร์ Intent นั้นใน Task ที่มีอยู่หรือใน Task ใหม่

"แอป ข" จำลองโดยโมดูล syntheticbackstackapp ซึ่งมี SyntheticBackStackAppActivity ที่คุณทำ Deep Link ไป โมดูลดังกล่าวจะแสดงวิธีสร้าง BackStack สังเคราะห์และวิธีจัดการ Task Stack อย่างเหมาะสมเพื่อรองรับทั้งปุ่มย้อนกลับและปุ่มขึ้น

วิธีใช้

ตรวจสอบว่าได้ติดตั้งทั้ง app หลักและ syntheticbackstackapp ในโปรแกรมจำลองหรืออุปกรณ์ที่เชื่อมต่อ ตรวจสอบว่า syntheticbackstackapp ที่ติดตั้งรองรับ ลิงก์ "www.nav3deeplink.com"

ในหน้า Landing Page ของสูตร ให้เลือกตัวกรองแล้วคลิกปุ่มเพื่อทำ Deep Link ระบบควรนำคุณไปยังกิจกรรมของ syntheticbackstackapp

วิธีการทำงาน

สูตรนี้เป็นไปตามหลักเกณฑ์ของ Deep Link ที่สรุปไว้ที่ นี่

วิธีดูลักษณะการทำงานของ Existing Task

  1. เปิด Deep Link โดยใช้ Task ปัจจุบัน
  2. ปัดขึ้นในอุปกรณ์เพื่อดูแอปทั้งหมดที่ใช้ล่าสุด
  3. สังเกตว่ากิจกรรมใหม่จะเปิดขึ้นภายในแอป Nav3Recipes
  4. คลิกปุ่มย้อนกลับเพื่อกลับไปยังกิจกรรมเดิม
  5. ทำซ้ำขั้นตอนที่ 1
  6. คลิกปุ่มขึ้นเพื่อไปที่หน้าจอระดับบน
  7. ปัดขึ้นในอุปกรณ์เพื่อดูแอปทั้งหมดที่ใช้ล่าสุด
  8. สังเกตว่ากิจกรรมใหม่จะเปิดขึ้นภายในแอป Nav3SyntheticBackStack

วิธีดูลักษณะการทำงานของ New Task

  1. เปิด Deep Link โดยใช้ Task ใหม่
  2. ปัดขึ้นในอุปกรณ์เพื่อดูแอปทั้งหมดที่ใช้ล่าสุด
  3. สังเกตว่ากิจกรรมใหม่จะเปิดขึ้นภายในแอป Nav3SyntheticBackStack
  4. คลิกปุ่มขึ้นหรือปุ่มย้อนกลับเพื่อไปที่หน้าจอระดับบน

การใช้งานหลัก

ฟังก์ชันตัวช่วยหลักสำหรับ navigateUp และการสร้าง BackStack สังเคราะห์ดูได้ที่นี่ ที่นี่

อ่านเพิ่มเติม

ดูคู่มือ Deep Link เพื่อดูคำแนะนำที่ ครอบคลุมเกี่ยวกับหลักการทำ Deep Link และวิธีใช้หลักการดังกล่าวใน Navigation 3

package com.example.nav3recipes.deeplink.handlerequests.syntheticbackstack

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.core.net.toUri
import androidx.lifecycle.compose.dropUnlessResumed
import com.example.nav3recipes.common.deeplink.EntryScreen
import com.example.nav3recipes.common.deeplink.LIST_FIRST_NAMES
import com.example.nav3recipes.common.deeplink.LIST_LOCATIONS
import com.example.nav3recipes.common.deeplink.MenuDropDown
import com.example.nav3recipes.common.deeplink.PaddedButton
import com.example.nav3recipes.common.deeplink.TextContent
import com.example.nav3recipes.ui.setEdgeToEdgeConfig

internal const val ADVANCED_PATH_BASE = "https://www.nav3deeplink.com"

/**
 * The recipe entry point that allows users to create a deep link and make a request with it.
 *
 * **HOW THIS RECIPE WORKS** This recipe simulates a real-world scenario where "App A" deep links
 * into "App B".
 *
 * "App A" is simulated by this current module `syntheticbackstack`, which
 * contains the [SyntheticBackStackDeepLinkActivity] that allows you to create a deeplink intent and
 * trigger that in either the existing Task, or in a new Task.
 *
 * "App B" is simulated by the module `syntheticbackstackapp`, which contains
 * the SyntheticBackStackAppActivity that you deeplink into. That module shows you how to build a synthetic backStack
 * and how to manage the Task stack properly in order to support both Back and Up buttons.
 *
 * See the [README](README.md) file of current module for more info on advanced deep linking.
 */
class SyntheticBackStackDeepLinkActivity: ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        setEdgeToEdgeConfig()
        super.onCreate(savedInstanceState)

        setContent {
            EntryScreen("Sandbox - Build Your Deeplink Intent") {
                val initFirstName = MENU_OPTIONS_FIRST_NAME.values.first().first()
                val initLocation = MENU_OPTIONS_LOCATION.values.last().first()
                val initTaskStack = MENU_OPTIONS_TASK_STACK.values.first().first()
                var firstName by remember { mutableStateOf(initFirstName) }
                var location by remember { mutableStateOf(initLocation) }
                var taskStack by remember { mutableStateOf(initTaskStack) }

                // select first name
                MenuDropDown(
                    menuOptions = MENU_OPTIONS_FIRST_NAME,
                ) { _, selected ->
                    firstName = selected
                }

                // select first name
                MenuDropDown(
                    menuOptions = MENU_OPTIONS_LOCATION,
                ) { _, selected ->
                    location = selected
                }

                // select current task stack or build new task stack
                MenuDropDown(
                    menuOptions = MENU_OPTIONS_TASK_STACK,
                ) { _, selected ->
                    taskStack = selected
                }

                // build final deeplink URL and Intent
                val finalUrl = "${ADVANCED_PATH_BASE}/user/$firstName/$location"

                // display Intent info
                val flagString = if (taskStack == TAG_NEW_TASK) {
                    "Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK"
                } else "<none>"
                val intentString = """
                    | Final Intent:
                    | data = "$finalUrl"
                    | action = Intent.ACTION_VIEW
                    | flags = $flagString
                """.trimMargin()

                TextContent(intentString)

                // deeplink to target
                PaddedButton("Deeplink Away!", onClick = dropUnlessResumed {
                    val intent = Intent().apply {
                        data = finalUrl.toUri()
                        action = Intent.ACTION_VIEW
                        if (taskStack == TAG_NEW_TASK) {
                            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                        }
                    }

                    startActivity(intent)
                })
            }
        }
    }
}

private const val TAG_FIRST_NAME = "firstName"
private const val TAG_LOCATION = "location"
private const val TAG_TASK_STACK = "Task stack"
private const val TAG_CURRENT_TASK = "Use Current Task Stack"
private const val TAG_NEW_TASK = "Start New Task Stack"

private val MENU_OPTIONS_FIRST_NAME = mapOf(
    TAG_FIRST_NAME to LIST_FIRST_NAMES
)

private val MENU_OPTIONS_LOCATION = mapOf(
    TAG_LOCATION to LIST_LOCATIONS
)

private val MENU_OPTIONS_TASK_STACK = mapOf(
    TAG_TASK_STACK to listOf(TAG_CURRENT_TASK, TAG_NEW_TASK),
)