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

相关文章

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关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定