Rezept für synthetischen BackStack für Deeplinks

In diesem Rezept wird gezeigt, wie Sie die Prinzipien der Navigation im Kontext von Deeplinks anwenden, indem Sie einen synthetischen BackStack und Aufgaben-Stacks verwalten.

Rezeptstruktur

In diesem Rezept wird ein reales Szenario simuliert, in dem „App A“ einen Deeplink zu „App B“ enthält.

„App A“ wird durch das Modul syntheticbackstack simuliert, das die SyntheticBackStackDeepLinkActivity enthält. Damit können Sie einen Deeplink-Intent erstellen und ihn entweder in der vorhandenen Aufgabe oder in einer neuen Aufgabe auslösen.

"App B" wird durch das Modul syntheticbackstackapp simuliert, das die SyntheticBackStackAppActivity enthält, zu der Sie einen Deeplink erstellen. In diesem Modul wird gezeigt, wie Sie einen synthetischen BackStack erstellen und den Aufgaben-Stack richtig verwalten, um sowohl die Schaltflächen „Zurück“ als auch „Nach oben“ zu unterstützen.

Verwendung

Achten Sie darauf, dass sowohl die Haupt-App app als auch syntheticbackstackapp auf dem Emulator oder dem verbundenen Gerät installiert sind. Achten Sie darauf, dass die installierte syntheticbackstackapp unterstützt den "www.nav3deeplink.com" Link.

Wählen Sie auf der Landingpage des Rezepts die Filter aus und klicken Sie auf die Schaltfläche, um einen Deeplink zu erstellen. Sie sollten zur Aktivität von syntheticbackstackapp weitergeleitet werden.

So gehts

Das Rezept folgt der hier zusammengefassten Deeplink-Anleitung.

So sehen Sie das Verhalten von Existing Task:

  1. Deeplink mit aktueller Aufgabe öffnen
  2. Wischen Sie auf dem Gerät nach oben, um alle zuletzt verwendeten Apps zu sehen.
  3. Die neue Aktivität wird in der Nav3Recipes-App geöffnet.
  4. Klicken Sie auf den Button „Zurück“, um zur ursprünglichen Activity zurückzukehren.
  5. Wiederholen Sie Schritt 1.
  6. Klicken Sie auf die Schaltfläche „Nach oben“, um zum übergeordneten Bildschirm zu wechseln.
  7. Wischen Sie auf dem Gerät nach oben, um alle zuletzt verwendeten Apps zu sehen.
  8. Die neue Aktivität wird jetzt in der Nav3SyntheticBackStack-App geöffnet.

So sehen Sie das Verhalten von New Task:

  1. Deeplink mit neuer Aufgabe öffnen
  2. Wischen Sie auf dem Gerät nach oben, um alle zuletzt verwendeten Apps zu sehen.
  3. Die neue Aktivität wird in der Nav3SyntheticBackStack-App geöffnet.
  4. Klicken Sie auf die Schaltfläche „Nach oben“ oder „Zurück“, um zum übergeordneten Bildschirm zu wechseln.

Kernimplementierung

Die wichtigsten Hilfsfunktionen für navigateUp und zum Erstellen eines synthetischen BackStacks finden Sie hier

Weitere Informationen

In der Deeplink-Anleitung finden Sie eine umfassende Anleitung zu den Prinzipien von Deeplinks und wie Sie sie in Navigation 3 anwenden.

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