You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Navigation Component:隐式DeepLink跳转至C页面后的返回栈配置问题

解决DeepLink跳转至C页面后返回至B页面的问题

核心思路很明确:通过DeepLink打开C页面时,必须手动构建包含B页面的返回栈,让系统返回逻辑识别B是C的直接前置页面,而非默认的A页面。以下是主流平台的具体实现方案:

Android 平台

方式1:手动构建Activity栈

在处理DeepLink的入口(比如Launcher Activity或者专门的DeepLink处理Activity)中,先创建B页面的Intent,再创建C页面的Intent,然后用startActivities()一次性启动这两个页面,这样栈结构就会是B -> C:

// 处理DeepLink的代码块
val bIntent = Intent(this, BActivity::class.java).apply {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
val cIntent = Intent(this, CActivity::class.java)
startActivities(arrayOf(bIntent, cIntent))

方式2:使用Jetpack Navigation

如果项目用了Jetpack Navigation,可以在导航图中给C页面的DeepLink配置关联的返回栈规则:

<fragment
    android:id="@+id/cFragment"
    android:name="com.example.CFragment">
    <deepLink app:uri="your_app_scheme://c_page" />
    <action
        android:id="@+id/action_deeplink_to_c"
        app:destination="@id/cFragment"
        app:popUpTo="@id/bFragment"
        app:popUpToInclusive="false" />
</fragment>

这样通过DeepLink打开C时,Navigation会自动将B页面加入返回栈,点击返回就会回到B。

iOS 平台

方式1:UIKit 手动构建导航栈

在处理DeepLink的回调(比如application(_:open:options:))中,先实例化B和C页面,再将它们依次加入导航控制器的栈中:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // 判断是跳转到C的DeepLink
    if url.scheme == "your_app_scheme" && url.host == "c_page" {
        let bVC = BViewController()
        let cVC = CViewController()
        let navVC = UINavigationController(rootViewController: bVC)
        navVC.pushViewController(cVC, animated: true)
        window?.rootViewController = navVC
        window?.makeKeyAndVisible()
        return true
    }
    return false
}

方式2:SwiftUI 配置NavigationStack路径

如果用SwiftUI,可以通过控制NavigationStack的path来构建包含B和C的栈:

@main
struct YourApp: App {
    @State private var navPath: [String] = []
    
    var body: some Scene {
        WindowGroup {
            NavigationStack(path: $navPath) {
                // 根据路径展示对应页面
                switch navPath.last {
                case "B":
                    BView()
                case "C":
                    CView()
                default:
                    AView()
                }
            }
            .onOpenURL { url in
                // 解析DeepLink,设置导航路径
                if url.host == "c_page" {
                    navPath = ["B", "C"]
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者mrzbn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 12:12:53