Android APP在debug模式下,如何避免activity被系统destroy

2024-05-25 20:52

本文主要是介绍Android APP在debug模式下,如何避免activity被系统destroy,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在调式APP时发现,如果在APP运行时(比如:进入了某个子模块的某个列表项的详情页:InfoShowActivity),点击home按钮,返回主屏幕。然后再返回APP时,APP不会显示之前的界面(InfoShowActivity),而是会如同重新打开一般,显示主页(MainActivity)。之前的详情页会自动destroy。

更奇怪的是,点击home按钮,返回主屏幕时,Logcat中只检测到InfoShowActivity的onStop方法,并没有检测到onDestroy方法。也就是说,InfoShowActivity没有执行onDestroy方法就被destroy,然后消失了。

刚开始,我还以为是APP的内存占用过高,导致触发系统GC操作。但使用Android Studio Memory Profiler(View-Tool Windows-Profiler)进行监测,相对于我开发的其他的APP,并没有发现内存占用量有什么区别。

这时我想到,之前是debug模式下出的问题,是否可以测试一下正式安装后的情况。于是我打包APK,然后安装到手机。结果,这个问题却不再重现了!

对比之前的测试过程,只有在debug模式下才出问题;而在打包后就没问题了。我猜测,是否系统对于APP的处理方式,在debug模式和正式安装模式下,有所区别?

我忽然想到了Android Studio新加的Gemini助手。于是提出问题:

Why in debug mode, when press home button, then open app, the previous activity will destroy?

Gemini回答了我:原来,在debug模式下,点击home按钮时,Android系统会destroy当前的activity。这是因为系统为了我们调试方便,想要保证给我们看到的永远最新的activity,就会destroy之前的Activity。如果要避免activity被destroy,就需要在activity的配置中加入:

android:alwaysRetainTaskState="true"

折腾了几个小时,终于找到了原因。真需要感谢Gemini。

另外,这是Gemini的回复原文,供参考:

When you press the home button in debug mode, the Android system will destroy the current activity. This is because the Android system knows that you are debugging the app and it wants to make sure that you are always seeing the latest version of the app.
When you open the app again, the Android system will create a new instance of the activity. This is because the previous instance of the activity was destroyed when you pressed the home button.
This behavior is specific to debug mode. In release mode, the Android system will not destroy the current activity when you press the home button. This is because the Android system wants to preserve the user's experience.
If you want to prevent the current activity from being destroyed when you press the home button in debug mode, you can use the android:alwaysRetainTaskState attribute in your manifest file. This attribute tells the Android system to always retain the task state, even when the app is in the background.
For example, to prevent the MainActivity from being destroyed when you press the home button in debug mode, you would add the following attribute to the <activity> element for MainActivity in your manifest file:

<activityandroid:name=".MainActivity"android:alwaysRetainTaskState="true"><!-- ... -->
</activity>

However, it is important to note that using the android:alwaysRetainTaskState attribute can have a negative impact on performance. This is because the Android system will need to keep more memory in use to retain the task state. Therefore, you should only use this attribute if you are absolutely sure that you need to prevent the current activity from being destroyed.

这篇关于Android APP在debug模式下,如何避免activity被系统destroy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1002600

相关文章

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

Linux系统之主机网络配置方式

《Linux系统之主机网络配置方式》:本文主要介绍Linux系统之主机网络配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、查看主机的网络参数1、查看主机名2、查看IP地址3、查看网关4、查看DNS二、配置网卡1、修改网卡配置文件2、nmcli工具【通用

Linux系统之dns域名解析全过程

《Linux系统之dns域名解析全过程》:本文主要介绍Linux系统之dns域名解析全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、dns域名解析介绍1、DNS核心概念1.1 区域 zone1.2 记录 record二、DNS服务的配置1、正向解析的配置

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式