Android使用scheme方式唤醒处于后台时的App场景

场景:甲App唤醒处于后台时的乙App的目标界面Activity,且乙App的目标界面Activity处于最上层,即已经打开状态,要求甲App使用scheme唤醒乙App时,达到跟从桌面icon拉起App效果一致,不能出现只拉起了乙App的目标界面Activity在甲App中,并且拉起后,乙App目标界面Activity的状态,跟处于后台时的一致,不能重新走onCreate,而是只走onReStart、onStart、onResume生命周期。

一、方式一

1、甲App跳转intent配置如下:

java 复制代码
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("schemeurltest://hostdemo"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);

其中schemeurltest://hostdemo为乙App的目标界面scheme地址;

注意:intent必须添加:intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED),否则只会在甲App中拉起乙App的目标界面Activity,而不是唤醒整个乙App。

2、乙App的目标界面Activity清单文件设置如下:

html 复制代码
<activity android:name="com.xxx.yyy.TestActivity"
    android:exported="true"
    android:theme="@style/TransparentDialog"
    android:screenOrientation="behind">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="schemeurltest"
            android:host="hosttest"/>
    </intent-filter>
</activity>

注意这里不用配置启动模式 android:launchMode=""

二、方式二

1、甲App跳转intent配置如下:

java 复制代码
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("schemeurltest://hostdemo"));
startActivity(intent);

其中 schemeurltest://hostdemo 为乙App的目标界面scheme地址;

注意这里不用设置intent的Flags

2、乙App的目标界面Activity清单文件设置如下:

html 复制代码
<activity android:name="com.xxx.yyy.TestActivity"
    android:exported="true"
    android:theme="@style/TransparentDialog"
    android:launchMode="singleTask"
    android:screenOrientation="behind">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="schemeurltest"
            android:host="hosttest"/>
    </intent-filter>
</activity>

注意这里启动模式必须配置为:android:launchMode="singleTask"

三、两种方式的区别

方式一,当启动intent设置为:intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED),可以不用设置启动App目标界面Activity的启动模式。

方式二,当启动intent不设置Flags时,则需要设置启动App目标界面Activity的启动模式为:android:launchMode="singleTask"。

四、不配置会怎样

如果不配置以上两种方式,那么在甲App里唤醒起来的乙App的目标界面Activity会是下面这样:

相关推荐
良逍Ai出海40 分钟前
OpenClaw 新手最该先搞懂的 2 套命令
android·java·数据库
hindon1 小时前
一文读懂 ViewModel
android
程序员JerrySUN1 小时前
别再把 HTTPS 和 OTA 看成两回事:一篇讲透 HTTPS 协议、安全通信机制与 Mender 升级加密链路的完整文章
android·java·开发语言·深度学习·流程图
音视频牛哥1 小时前
Android平台GB28181设备接入模块架构解析、功能详解与典型应用场景分析
android·android gb28181·gb28181安卓端·gb28181对接·gb28181设备·gb28181语音广播·安卓gb28181设备对接
叁两2 小时前
前端开发如何快速上手安卓APP开发?
android
guodashen0072 小时前
在安卓端启动一个服务器接口,用于接收post请求的json数据
android·服务器·json
hindon2 小时前
一文读懂Android 中的 MVC、MVP、MVVM
android
漏刻有时2 小时前
CentOS 不定时 OOM 根治方案:PHP-FPM 进程管控 + Swap 扩容 + 全维度监控
android·centos·php
恋猫de小郭4 小时前
Android 性能迎来提升:内核引入 AutoFDO 普惠所有 15-16 设备
android·前端·flutter
CS_Zero4 小时前
Android ADB调试工具使用简记
android·adb