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会是下面这样:

相关推荐
慕伏白25 分钟前
【慕伏白】Android Studio 无线调试配置
android·ide·android studio
低调小一1 小时前
Kuikly 小白拆解系列 · 第1篇|两棵树直调(Kotlin 构建与原生承载)
android·开发语言·kotlin
跟着珅聪学java1 小时前
spring boot 整合 activiti 教程
android·java·spring
川石课堂软件测试3 小时前
全链路Controller压测负载均衡
android·运维·开发语言·python·mysql·adb·负载均衡
2501_915921433 小时前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915921433 小时前
苹果软件混淆与 iOS 应用加固白皮书,IPA 文件加密、反编译防护与无源码混淆方案全解析
android·ios·小程序·https·uni-app·iphone·webview
倔强的石头1064 小时前
【Linux指南】Linux命令行进度条实现原理解析
android·linux
yeziyfx5 小时前
Android Studio制作.9图(点9图/9Patch图)
android·ide·android studio
Chejdj6 小时前
StateFlow、SharedFlow 和LiveData区别
android·面试
蓝不蓝编程6 小时前
AI编程作品:Android 极简倒计时应用
android·ai编程