Android 不要保持活动 - 这是为了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22400859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:55:36  来源:igfitidea点击:

Don't keep activities - What is it for?

androidandroid-activity

提问by Sebastiano

The title is pretty self-explanatory. I understand what this developer option does.

标题是不言自明的。我了解此开发人员选项的作用。

What I don't understandare the following points:

不明白的是以下几点:

  1. Why was that option introduced, in the first place?
  2. After all the changes that the framework has seen throughout the years, is it still useful?
  1. 为什么首先引入该选项?
  2. 框架经历了这么多年的变化,它还有用吗?

I am eager to know the reasons behind this option.

我很想知道这个选项背后的原因。

采纳答案by Nicola Miotto

I believe it's a feature used for debugging purpose.

我相信这是用于调试目的的功能。

From the Titanium doc:

钛文档

Don't keep activitiesunder the Developer Options menu. When this option is enabled, the Android OS will destroy an activity as soon as it is stopped. It is intended to help developers debug their apps. For example, it can simulate the case that Android will kill an activity in the background due to memory pressure. In normal use, it is not recommended to turn this option on because this may lead to unexpected issues on the apps, such as freezes, force closes and reboots.

不要在开发人员选项菜单下保留活动。启用此选项后,Android 操作系统将在活动停止后立即销毁活动。它旨在帮助开发人员调试他们的应用程序。例如,它可以模拟Android由于内存压力而在后台杀死一个活动的情况。在正常使用中,不建议开启此选项,因为这可能会导致应用程序出现意外问题,例如死机、强制关闭和重新启动。

It sounds like it basically helps testing deterministicallyhow your app behaves when the OS shuts it down due to any reason (out of memory and so on).

听起来它基本上有助于确定性地测试您的应用程序在操作系统因任何原因(内存不足等)关闭时的行为方式。

So, this replied to point 1. Point 2 is: Yes, I guess :)

所以,这回答了第 1 点。第 2 点是:是的,我猜:)

EDIT: further references

编辑:进一步参考

回答by Kevin Krumwiede

The Android framework maydestroy your activity any time it's in the background or backstack, and you should write your activities so they behave correctly when this happens. Exactly what that entails varies depending on what the activity does, but it generally includes implementing onSaveInstanceState(...)and restoring any previous state in onCreate(...).

Android 框架可能会在任何时候在后台或后台销毁您的 Activity,您应该编写您的 Activity,以便它们在发生这种情况时能够正确运行。具体需要什么取决于活动做什么,但它通常包括实现onSaveInstanceState(...)和恢复onCreate(...).

The "don't keep activities" developer option simply changes the framework's behavior so it will always destroy your activity when it goes into the background or backstack. This allows you to test how your activity responds to what is normally a rare occurrence.

“不保留活动”开发人员选项只是更改框架的行为,因此当它进入后台或后台时,它总是会破坏您的活动。这允许您测试您的活动如何响应通常很少发生的事件。

A linkcited in another answer says:

另一个答案中引用的链接说:

In normal use, it is not recommended to turn this option on because this may lead to unexpected issues on the apps, such as freezes, force closes and reboots.

在正常使用中,不建议开启此选项,因为这可能会导致应用程序出现意外问题,例如死机、强制关闭和重新启动。

This is incorrect. If your activities are written properly, the only effect of having "don't keep activities" turned on should be (possibly) slightly higher battery and CPU usage from constantly saving and restoring persistent state. Any apps that exhibit "unexpected issues" or force closes when this option is on are broken and need to be fixed. As a developer, I habitually leave "don't keep activities" turned on all the time. I've seen a lot of buggy apps, even some of Google's own. But it's never caused a reboot, and I don't think there's any way it could.

这是不正确的。如果您的 Activity 编写正确,那么打开“不保留活动”的唯一效果应该是(可能)通过不断保存和恢复持久状态而略微提高电池和 CPU 使用率。任何在此选项打开时表现出“意外问题”或强制关闭的应用程序都已损坏,需要修复。作为一名开发人员,我习惯性地让“不要保持活动”一直处于开启状态。我见过很多有问题的应用程序,甚至是 Google 自己的一些应用程序。但它从未导致重新启动,我认为没有任何办法。

回答by Steve GS

I had at least two issues when this was enabled:

启用此功能后,我至少遇到了两个问题:

1) I was getting an error "Unable to complete previous operation due to low memory" that prevented uploading of any attachment to a website using Chrome, but not with Firefox. I found elsewhere that enabling the "Don't keep activities open" option could have been the problem - it was.

1) 我收到错误“由于内存不足而无法完成之前的操作”,该错误阻止使用 Chrome 将任何附件上传到网站,但不能使用 Firefox。我在其他地方发现启用“不要保持活动打开”选项可能是问题所在 - 确实如此。

2) I was unable to move files to the SD card. Disabling the "Don't keep activities open" option got round this problem.

2) 我无法将文件移动到 SD 卡。禁用“不要保持活动打开”选项解决了这个问题。

Also found elsewhere it can slow things up generally because caching is compromised.

在其他地方也发现它通常会减慢速度,因为缓存受到损害。

Sometimes apps that I had moved to the SD card suddenly 'greyed out' and became inaccessible. Rebooting solved this, but was becoming an irritation. Too soon to tell if that was another problem caused by this 'feature'.

有时,我已移至 SD 卡的应用程序会突然“变灰”并且无法访问。重新启动解决了这个问题,但变得很烦人。判断这是否是此“功能”引起的另一个问题还为时过早。

These problems (and issues with the microphone in Zoom) all started within the last month. I don't know how DKAO ever got enabled, but it's a no-no as far as I'm concerned.

这些问题(以及 Zoom 中麦克风的问题)都是在上个月开始的。我不知道 DKAO 是如何启用的,但就我而言,这是一个禁忌。