Android的破坏活动,终止进程 [英] Android destroying activities, killing processes

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

问题描述

您好我想知道如何Android正在管理内存,我无法找到precise答案的任何地方。 假设我有5项活动上的当前活动栈的应用程序(4停止和1个恢复),没有连接服务。我preSS HOME按钮,这样我所有的活动都停止了。 我开始有些其他消耗内存的应用和整体设备存储器开始为低。而问题是,

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. 可以在系统摧毁只有一个或我的一些活动,以恢复内存?
  2. 威尔系统杀死我的应用程序的全过程?将所有的活动中很好地破坏了?
  3. 当我回到我的应用程序,当它被完全杀死,会发生什么?它会从beggining(如首次启动)开始还是会尝试恢复活动,以previeous状态/如果是的话 - 是它只是一个在堆栈的顶部或全部

更新:

在问这个问题,我已经看到了活动的生命周期了几次,但它并没有回答我的问题。 我做了一些测试,我有一些答案。 停止处理DDMS是一个线索进行测试。

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.

我没有测试的回答问题1,但作为指导说:

如果活动被暂停或停止,系统可以删除活动   从内存或者要求它完成,或者干脆杀了   流程。

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.

似乎一个活动的一个或多个可以被轻轻地(用方法的onDestroy)而不杀死过程破坏。你会简单地得到(的onCreate +包)又回到了他们的时候。

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.

第二个问题的回答:

YES。一般系统杀死的全过程,这意味着所有的数据,包括活动和静态字段被破坏。不这样做很好 - 你不会得到的onDestroy或finialize(),您的任何暂停的/停止活动。这就是为什么saveInstanceState()仅仅是在onPause方法之前调用。的onPause基本上是最后一个方法,你应该保存一些事情,因为这种方法后,你永远无法看到的onStop或的onDestroy。系统可以直接杀死进程销毁所有对象不管他们持有和任何他们正在做的事情。

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.

问题3回答:

当你回到一个被杀的应用程序会发生什么事?

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

  • 此前的Andr​​oid 2.2 - 应用程序将从beggining开始,与发射活动
  • 从2.2开始 - 系统将恢复previous应用程序的状态。这是什么意思?这意味着最后一个可见的活动将被重新创建(的onCreate +包)。将与活动堆栈发生什么情况?堆栈是好的,但是,这一切活动都死了。他们每个人都将被重新创建(的onCreate +包),当你回到它的后退按钮。 还有一件事有关:
  • 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:

通常情况下,系统会清除任务(删除所有活动从   栈之上的根活性)在某些情况下,当用户   重新选择从主屏幕上的任务。通常,这是如果做   用户还没有访问过的任务的一个特定量的时间,如   30分钟。

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.

结论?

  1. 请不要以为处理活动的旋转即可解决问题 通过机器人:configChanges =定位。当你做,你会 让你甚至不知道其他许多问题。
  2. 测试您的应用程序DDMS - 停止的过程按钮。 看到这个
  3. 使用静态变量时要小心。不要以为当您在活动1初始化它们 - 你将他们的活动2.唯一安全的地方来初始化全局静态将应用程序类初始化
  4. 请记住,你可能永远也看不到的onStop或的onDestroy。关闭文件/数据库,停止下载者在的onPause。当你想要的应用程序做一下BG - 使用前景色服务
  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.

这将是它...希望我帮助我的ESSEY:)

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

推荐答案

首先,请看看这个:

的onPause()当系统要开始恢复一个叫   previous活动。这通常用于提交未保存的更改   持久性数据,停止动画和可能其他东西   耗费CPU的这个方法,等实现必须非常快   因为下一个活动将不被恢复,直到此方法   回报。后面是onResume(),如果该活动返回到   前,或的onStop()如果成为对用户不可见。

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()当活动不再对用户可见的调用,因为其他活动已经恢复并且覆盖该   一。这种情况可能发生或者是因为一个新的活动正在启动,   现有将被带入在这其中的前面,或者这个人是   遭到破坏。后跟onRestart()如果此活动   回来与用户,或的onDestroy()如果​​此活动交互   要走了。

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.

所以,当你preSSHOME您的设备上按钮,您当前的前景活动放到的onPause()然后的onStop (),其他4仍应的onStop()

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()

据谷歌的文档:

      
  • 如果在屏幕的前景的活性(在堆栈的顶部)时,它是活动的或跑步。
  •   
  • 如果活动已经失去焦点,但仍清晰可见(即,一个新的非全尺寸或透明的活动都集中在顶部您   活性),它被暂停。一个暂停的activity是完全活着(它   保持所有的状态和成员信息,并保持连接到   窗口管理器),但可以通过系统在极端低内存被杀死   的情况。
  •   
  • 如果活动被另一个活动完全遮蔽,它被停止。它仍然保留了所有状态和成员信息,但是,   它不再对用户可见所以其窗口被隐藏,并将其   往往会被当内存被其他地方所需要的系统被杀死。
  •   
  • 如果活动被暂停或停止,该系统可以通过拖放从内存中的活动,要求它完成,或者干脆杀   它的过程。当再次显示给用户,它必须是   完全重新启动并恢复到previous状态。
  •   
  • 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:

流程生命周期 3.背景活动(活动是不可见的用户和已暂停)已不再是重要的,所以   该系统可以安全地杀死它的进程,以回收内存为其他   前景或可见的过程。如果其进程需要被杀害,   当用户导航回到活动(使其在可见   屏幕再次),它的onCreate(束)方法将被调用的   savedInstanceState它previously在提供   的onSaveInstanceState(束),以便它可以在相同的重新启动本身   国家作为用户上次离开它。

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.

以上所有报价都来自于: Android开发人员参考:活动

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

据证实,该系统能摧毁非acitve活动和回收回忆,当你推出一些消耗内存的应用程序。而且可以实现这样的: isFinishing()在您的活动,然后使用杀在DDMS按钮,能够检测到你的活动正在下降体系。但我想,系统首先会破坏年龄最大的一位。然而,它是没有意义的,以使其他活动时,启动活动已被回收。

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.

更新

下面的一些意见,我发现从<一个href="http://answers.oreilly.com/topic/2692-android-programming-understanding-the-activity-life-cycle/">here:

Here's some opinions I found from here:

停止状态

当一个活动是不可见的,但仍然在内存中,我们说这是一个   停止状态。停止活动可以被带回前线   重新成为运行活动。或者,也可能被破坏并除去   从内存中。

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天全站免登陆