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

相关文章

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操