Android销毁活动,杀死进程 [英] Android destroying activities, killing processes

查看:24
本文介绍了Android销毁活动,杀死进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 Android 是如何管理内存的,但我在任何地方都找不到准确的答案.假设我有一个在当前活动堆栈上有 5 个活动的应用程序(4 个停止,1 个恢复),没有连接服务.我按下 HOME 按钮,这样我的所有活动都停止了.我启动了一些其他内存消耗应用程序,并且整体设备内存开始变低.问题是

...我的申请会怎样?

  1. 系统能否仅销毁我的一项或多项活动以恢复内存?
  2. 系统会终止我的应用程序的整个进程吗?所有活动都会被很好地销毁吗?
  3. 当我回到我的应用程序完全终止时会发生什么?它会从开始(如第一次开始)开始还是会尝试将活动恢复到以前的状态/如果是 - 它只是堆栈顶部的一个还是所有的?

更新:

在问这个问题之前,我已经看过几次 Activity 生命周期,但它没有回答我的问题.我做了一些测试,我有一些答案.DDMS 中的停止进程"是测试的线索.

我还没有测试过问题 1 的答案,但正如指南所说:

<块引用>

如果活动暂停或停止,系统可以删除活动通过要求它完成或简单地杀死它来从记忆中过程.

似乎可以在不杀死进程的情况下轻轻地销毁一个或多个活动(使用onDestroy方法).当你回到他们那里时,你只会得到 (onCreate + bundle).

问题 2 答案:

是的.通常系统会杀死整个进程,这意味着包括活动和静态字段在内的所有数据都被销毁.这做得不好 - 对于任何暂停/停止的活动,您都不会获得 onDestroy 或 finalize() .这就是在 onPause 方法之前调用 saveInstanceState() 的原因.onPause 基本上是您应该保存内容的最后一个方法,因为在此方法之后您将永远看不到 onStop 或 onDestroy.系统可以终止进程,销毁所有对象,无论它们持有什么,正在做什么.

问题 3 答案:

当你回到被杀死的应用程序时会发生什么?

  • 在 Android 2.2 之前 - 应用程序将从头开始,带有启动器活动.
  • 从 2.2 开始 - 系统将恢复之前的应用程序状态.这是什么意思?这意味着将重新创建最后一个可见的活动(onCreate + bundle).活动堆栈会发生什么?堆栈很好,但其上的所有活动都已死亡.当您使用后退按钮返回时,它们中的每一个都将被重新创建(onCreate + bundle).还有一件事:
<块引用>

通常情况下,系统会清除一个任务(从堆叠在根活动之上)在某些情况下,当用户从主屏幕重新选择该任务.通常,这是在以下情况下完成的用户在一定时间内没有访问任务,例如30 分钟.

结论?

  1. 不要认为处理活动轮换问题可以解决通过 android:configChanges="orientation".当你这样做时,你会遇到许多您甚至不知道的其他问题.
  2. 使用 DDMS 测试您的应用程序 - 停止进程按钮.

    已确认当您启动一些内存消耗应用程序时,系统可以销毁非活动活动并回收内存.并且您可以在您的活动中实现类似: isFinishing() ,然后使用 DDMS 中的kill"按钮来检测系统正在删除您的哪些活动.但我猜系统会先销毁最旧的.然而,当Launch Activity"被回收时,保留其他 Activity 是没有意义的.

    更新

    以下是我从 此处找到的一些意见:

    <块引用>

    停止状态

    当一个活动不可见,但仍在内存中时,我们说它在一个停止状态.停止的活动可以重新回到前面再次成为跑步活动.或者,它可以被破坏和移除凭记忆.

    系统将活动保持在停止状态,因为它是用户可能仍想回到这些活动中很快,重新开始一个停止的活动比从头开始一项活动.那是因为我们已经拥有了所有加载到内存中的对象,只需将其全部加载到前景.

    可以随时从内存中删除已停止的活动.

    Hi I'm wondering how Android is managing memory and I can't find precise answer anywhere. Let's assume I have an application with 5 activities on current activity stack (4 are stopped and 1 is resumed), there is no service connected. I press HOME button so that all of my activities are stopped. I start some other memory consuming application and overall device memory is starting to be low. And the question is

    ... What will happen to my application?

    1. Can system destroy only one or some of my activities to recover memory?
    2. Will system kill the whole process of my application? Will all activities be nicely destroyed?
    3. What will happen when I get back to my application when it was totally killed? Will it start from beggining (like the first start) or will it try to recover activities to previeous state / if yes - is it only the one on the top of the stack or all of them?

    UPDATE:

    Before asking this question I've seen Activity lifecycle a few times but it doesn't have answers to my questions. I made some tests and I have some answers. "Stop process" in DDMS was a clue for testing.

    I haven't tested answer for question 1, but as guide says:

    If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process.

    It seems that one or more of the activities can be destroyed gently(with onDestroy method) without killing the process. You will simply get (onCreate + bundle) when getting back to them.

    Question 2 answer:

    YES. Generally system kills the whole process this means all data including activities and static fields are destroyed. This is NOT done nicely - you won't get onDestroy or finialize() for any of your paused/stopped activities. This is why saveInstanceState() is called just before onPause method. onPause is basically the last method where you should save something because after this method you could never see onStop or onDestroy. System can just kill the process destroying all of your objects whatever they hold and whatever they are doing.

    Question 3 answer:

    What will happen when you get back to a killed application?

    • Prior to Android 2.2 - application will start from the beggining, with launcher activity.
    • Starting from 2.2 - system will restore the previous application state. What does it mean? It means that last visible activity will be recreated (onCreate + bundle). What will happen with activity stack? Stack is fine but all activities on it are dead. Each of them will be recreated (onCreate + bundle) when you get back to it with back button. There is one more thing about that:

    Normally, the system clears a task (removes all activities from the stack above the root activity) in certain situations when the user re-selects that task from the home screen. Typically, this is done if the user hasn't visited the task for a certain amount of time, such as 30 minutes.

    Conclusion?

    1. Don't think that handling activity rotation problems can be solved by android:configChanges="orientation". When you do that you will get many other problems that you are not even aware of.
    2. Test your application with DDMS - Stop process button. See This
    3. Be careful when using static variables. Don't think that when you initialized them in activity 1 - you will have them initialized in activity 2. The only safe place to initialize global statics would be Application class.
    4. Remember that you may never see onStop or onDestroy. Close files/databases, stop downloaders in onPause. When you want app to do something in BG - use foreground Service.

    That would be it ... Hope I helped with my essey :)

    解决方案

    First please have a look at this:

    onPause() Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

    onStop() Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed. Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

    So, when you press "HOME" button on your device, your current foreground activity is put onto onPause() then onStop(), the other 4 should remain onStop()

    According to Google's Documents:

    • If an activity in the foreground of the screen (at the top of the stack), it is active or running.
    • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
    • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
    • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

    And, for the process lifecycle:

    Process Lifecycle 3. A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

    All the quotes above are come from: Android Developers Reference: Activity

    It is confirmed that the system can destroy non-acitve activities and recycle memories when you launched some memory consuming applications. And you can implement like: isFinishing() in your activity and then using "kill" button in DDMS to detect which of your activities is being dropped by system. But I guess the system will destroy the oldest one first. However it is no point to keep other activities when the "Launch Activity" has been recycled.

    UPDATE

    Here's some opinions I found from here:

    Stopped state

    When an activity is not visible, but still in memory, we say it’s in a stopped state. Stopped activity could be brought back to the front to become a Running activity again. Or, it could be destroyed and removed from memory.

    The system keeps activities around in a stopped state because it is likely that the user will still want to get back to those activities some time soon, and restarting a stopped activity is far cheaper than starting an activity from scratch. That is because we already have all the objects loaded in memory and simply have to bring it all up to the foreground.

    Stopped activities can be removed from memory at any point.

    这篇关于Android销毁活动,杀死进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆