android中“不保持活动”的主要优点和缺点是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21227623/
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
Whats the main advantage and disadvantage of "do not keep activities" in android
提问by BlueBerry - Vignesh4303
One query I would like to have if anyone could answer it like: Do not keep activities
options to be checked during testing android mobile application from developers options.
如果有人可以回答,我想要的一个问题是:Do not keep activities
在开发人员选项中测试 android 移动应用程序期间要检查的选项。
I used it in my application and found that my application behaves inappropriately and crashed when I switched ON Do not keep activities
in android.
我在我的应用程序中使用它,发现当我Do not keep activities
在 android 中打开时,我的应用程序行为不当并崩溃。
My questions were few :
我的问题很少:
1: How much this option will affect mobile applications?
1: How much this option will affect mobile applications?
2: What exactly does this do?
2: What exactly does this do?
It sounds like an app killer,I notice in the Developer Options there is a box that says Do not keep activities -destroy every activity as soon as the user leaves it
.
这听起来像是一个应用杀手,我注意到在开发者选项中有一个框,上面写着不要保留活动 -destroy every activity as soon as the user leaves it
。
Does this create any positive and or negative functionality on my apps?
这是否会对我的应用产生任何积极和/或消极的功能?
Does that mean if I open an app and as soon as I leave it, it actually closes that app and I wouldn't see it in the task manager to manually kill it? If so, isn't this a good thing to keep the RAM usage low?
这是否意味着如果我打开一个应用程序,一旦我离开它,它实际上会关闭该应用程序并且我不会在任务管理器中看到它来手动杀死它?如果是这样,保持较低的 RAM 使用率不是一件好事吗?
What were the advantages and disadvantages of using it while keeping Do not keep activities
,Kindly share the experience on it.
保留的同时使用它有什么优点和缺点Do not keep activities
,请分享它的经验。
采纳答案by Adam Radomski
How much this option will affect mobile applications?
此选项将在多大程度上影响移动应用程序?
If they are well writen this option will not affect them.
如果他们写得很好,这个选项不会影响他们。
What exactly does this do?
这究竟是做什么的?
If you have this option turned on only variables kept in activity with method onSaveInstanceState
will be saved when you go to another activity or app goes in background. All other variables will be removed immediately.
When this option is off there is possibility that these variable will be kept
如果您打开此选项,onSaveInstanceState
则当您转到另一个活动或应用程序进入后台时,只会保存活动中保留的变量和方法。所有其他变量将被立即删除。当此选项关闭时,这些变量可能会被保留
Does that mean if I open an app and as soon as I leave it, it actually closes that app and I wouldn't see it in the task manager to manually kill it?
这是否意味着如果我打开一个应用程序,一旦我离开它,它实际上会关闭该应用程序并且我不会在任务管理器中看到它来手动杀死它?
No it means that all not kept variables will be removed. When you in example press home button.
不,这意味着所有未保留的变量都将被删除。例如,当您按主页按钮时。
Does this create any positive and or negative functionality on my apps?
这是否会对我的应用产生任何积极和/或消极的功能?
No it only helps to develop application properly. It helps to predict unexpected situations.
不,它只会有助于正确开发应用程序。它有助于预测意外情况。
回答by Siddharth N
Do not Keep Activities is purely a developer option that will help you to check if
不要保留活动纯粹是一个开发人员选项,可以帮助您检查是否
- You have Saved the state of the activity, before it goes background.
- 在进入后台之前,您已经保存了活动的状态。
2?.? ?H?a?n?d?l?e?d? ?L?o?w? ?M?e?m?o?r?y? ?S?i?t?u?a?t?i?o?n?s? ?p?r?o?p?e?r?l?y?(?i?n? ?w?h?i?c?h? ?c?a?s?e? ?t?h?e? ?a?c?t?i?v?i?t?y? ?w?i?l?l? ?b?e? ?d?e?s?t?r?o?y?e?d?)?.? ?
2?.? ?H?a?n?d?l?e?d? ?低的??记忆??S?i?t?u?a?t?i?o?n?s? ?p?r?o?p?e?r?l?y?(?i?n??w?h?i?c?h??c?a?s?e??t?h?e? ?a?c?t?i?v?i?t?y??w?i?l?l??b?e??d?e?s?t?r?o?y?e?d? )?.? ?
Edit: This option does not emulate Low memory Situations. When the device experiences low memory, the system might ask the activity to Drop by calling Finish() or it may go ahead and kill the process completely, as the comment says.
编辑:此选项不模拟低内存情况。当设备遇到内存不足时,系统可能会通过调用 Finish() 来要求 Activity 删除,或者它可能会继续并完全终止进程,如注释所述。
It is still good to develop with this option enabled. You will have to properly code the onSaveInstanceState()
and onRestoreInstanceState()
methods. By doing this, even if the process gets killed, when the user navigates back to this activity, onCreate() will be called with the savedInstanceState that was saved in the onSaveInstanceState(Bundle)
method.
在启用此选项的情况下进行开发仍然很好。您必须正确编码onSaveInstanceState()
和onRestoreInstanceState()
方法。通过这样做,即使进程被终止,当用户导航回此活动时,也会使用onSaveInstanceState(Bundle)
方法中保存的 savedInstanceState 调用 onCreate() 。
回答by Kushal
ADVANTAGE:
优势:
Developer can check the abnormal behaviour of their application and fix the cases of low memory - framework kills the application
开发人员可以检查其应用程序的异常行为并修复 low memory - framework kills the application
DISADVANTAGE:
缺点:
If user has unknowingly enabled this option, then the device will work slow and every activities will be re-created across user navigation on his device. This will hinder user work
如果用户在不知情的情况下启用了此选项,则设备将运行缓慢,并且每个活动都将在其设备上的用户导航中重新创建。这会妨碍用户工作
Very good answer is given in xda developer forumabout usage of this option
xda 开发者论坛中给出了关于此选项使用的很好的答案
回答by Beloo
As an addition to answers above is another, not visible from the first look disadvantagethat you can test only activitydestroying/recreation issues with this option, but not the whole process of application recreation due to memory lack or other system conditions, because all independent from activity memory stays.
作为上述答案的补充是另一个从第一眼看不到的缺点,您只能使用此选项测试活动破坏/重新创建问题,但不能测试由于内存不足或其他系统条件而导致的应用程序重新创建的整个过程,因为所有这些都是独立的从活动记忆停留。
Imagine that you have some singletonon which your classes are dependent. After system have killed app your singletons will be also cleared and restored with beginning state in case you haven't implemented its restoring by yourself. So, despite of your activity view state & fields would be restored in case onSaveInstanceState
& onRestoreInstanceState
was implemented correctly, that's not guarantee a correct app behavior after restoring, even on the particular screen. That should be considered
想象一下,您有一些类所依赖的单例。在系统杀死应用程序后,您的单身人士也将被清除并以开始状态恢复,以防您自己没有实现其恢复。所以,尽管你的活动视图状态和领域会的情况下,恢复onSaveInstanceState
和onRestoreInstanceState
正确实施,这是不能保证正确的应用程序行为恢复后,即使在特定的屏幕上。那应该考虑
So to test such fully case you should stop application manually, but don't drop it from task manager. Easiest way - with stop red square button in android studio. And open app again.
因此,要测试这种完整的情况,您应该手动停止应用程序,但不要将其从任务管理器中删除。最简单的方法 - 在 android studio 中使用停止红色方形按钮。并再次打开应用程序。
回答by Androider
"Do not keep activities" advantage is that it emulate system low memory situation when it start killing different parts of the app. Your app must maintain this situation. Disadvantage is that this option is kind of stricted and kill only activities when there is no way emulate this for services
“不保留活动”的优势在于它在开始杀死应用程序的不同部分时模拟系统内存不足的情况。您的应用程序必须保持这种情况。缺点是这个选项有点严格,只有在没有办法为服务模拟这个时才杀死活动