gpt4 book ai didi

Android销毁 Activity ,杀死进程

转载 作者:IT老高 更新时间:2023-10-28 13:01:37 25 4
gpt4 key购买 nike

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

...我的申请会怎样?

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

更新:

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

我尚未测试问题 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.

似乎可以温和地销毁一个或多个 Activity (使用 onDestroy 方法)而不杀死进程。回到它们时,您将简单地得到 (onCreate + bundle)。

问题 2 答案:

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

问题 3 答案:

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

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

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. 不要以为处理 Activity 轮换问题就可以解决通过 android:configChanges="orientation"。当你这样做时,你会遇到许多您甚至不知道的其他问题。
  2. 使用 DDMS 测试您的应用程序 - 停止进程按钮。 See This
  3. 使用静态变量时要小心。不要以为当您在 Activity 1 中初始化它们时 - 您将在 Activity 2 中初始化它们。初始化全局静态变量的唯一安全位置是 Application 类。
  4. 请记住,您可能永远不会看到 onStop 或 onDestroy。关闭文件/数据库,在 onPause 中停止下载器。当您希望应用在 BG 中执行某些操作时 - 使用前台服务。

就是这样......希望我对我的 essey 有帮助 :)

最佳答案

首先请看一下:

img1

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.

因此,当您按下设备上的“HOME”按钮时,您当前的前台 Activity 将置于 onPause() 然后 onStop(),其他 4 个应保留onStop()

根据 Google 的文档:

  • 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.

并且,对于流程生命周期:

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 Developers Reference: Activity

确认当您启动一些内存消耗的应用程序时,系统可以销毁非 Activity Activity 并回收内存。你可以在你的 Activity 中实现像: isFinishing() ,然后使用 DDMS 中的“kill”按钮来检测你的哪些 Activity 被系统丢弃。但我猜系统会先销毁最旧的。但是,当“Launch Activity”被回收时,保留其他 Activity 是没有意义的。

更新

这是我从 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销毁 Activity ,杀死进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14375720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com