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

Android应用进程被杀后,Deeplink丢失URL无法跳转指定Fragment

核心问题

进程被杀后,从Chrome触发DeepLink启动应用时,MainActivity接收到的Intent无ACTION_VIEW标识及data数据,导致无法解析跳转目标;进程存活时可正常跳转,仅Android 14无此问题,已在onCreate()和onNewIntent()中处理DeepLink逻辑。

具体解决办法

1. 确保onNewIntent()中正确更新Intent

即使已实现onNewIntent(),需强制调用setIntent(intent)更新Activity的Intent实例,避免后续getIntent()获取旧的空数据:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent); // 关键步骤:替换当前Activity的Intent引用
    handleDeepLink(intent);
}

2. 从任务栈BaseIntent中恢复DeepLink数据

进程被杀后,系统重建的任务栈中,BaseIntent可能保留了原始DeepLink数据,可通过ActivityManager获取:

private void handleDeepLink() {
    Intent currentIntent = getIntent();
    // 先检查当前Intent是否有效
    if (isValidDeepLinkIntent(currentIntent)) {
        processDeepLink(currentIntent.getData());
        return;
    }

    // 当前Intent无效时,尝试从任务栈BaseIntent读取
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    if (am != null) {
        List<ActivityManager.AppTask> tasks = am.getAppTasks();
        if (!tasks.isEmpty()) {
            Intent baseIntent = tasks.get(0).getTaskInfo().baseIntent;
            if (isValidDeepLinkIntent(baseIntent)) {
                setIntent(baseIntent); // 更新Intent
                processDeepLink(baseIntent.getData());
            }
        }
    }
}

// 辅助方法:判断Intent是否为有效DeepLink
private boolean isValidDeepLinkIntent(Intent intent) {
    return intent != null && Intent.ACTION_VIEW.equals(intent.getAction()) && intent.getData() != null;
}

// 在onCreate()和onNewIntent()中调用handleDeepLink()

注:Android 11及以上需适配权限规则,可改用AppOpsManager替代GET_TASKS权限获取任务栈信息。

3. 调整Manifest配置

  • 确保DeepLink的intent-filter配置完整:
<activity android:name=".MainActivity"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="your-custom-scheme"
            android:host="your-host" />
    </intent-filter>
</activity>
  • 添加android:alwaysRetainTaskState="true",降低系统清理任务栈状态的概率:
<activity android:name=".MainActivity"
    android:launchMode="singleTop"
    android:alwaysRetainTaskState="true">

4. 临时验证启动模式兼容性

切换为standard启动模式测试,若进程被杀后Intent可正常携带数据,说明之前的singleTop/singleTask与系统Intent传递存在冲突,可结合任务栈管理逻辑调整启动模式。

5. 适配低版本系统Intent传递差异

针对Android 14以下版本,可在onCreate()中额外检查Intent的Extras,部分系统版本会将DeepLink数据存放在Extra字段中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    if (intent != null) {
        // 检查Extras中是否存在DeepLink数据
        Uri deepLinkUri = intent.getParcelableExtra(Intent.EXTRA_INTENT);
        if (deepLinkUri != null) {
            processDeepLink(deepLinkUri);
        } else {
            handleDeepLink();
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 14:25:56