【问题标题】:System call onPause and onStop系统调用 onPause 和 onStop
【发布时间】:2014-04-08 06:11:55
【问题描述】:

当系统调用onPauseonStop 活动生命周期方法时,我有一个困惑。我看到通过从我的活动导航到另一个应用程序,这里系统正在调用onPauseonStop,当我再次启动我的应用程序的另一个活动时,系统正在调用onPauseonStop 方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startActivity(new Intent(this, SecondActivity.class));
}

这里answer 说“当一个 Activity 将控制权转移到另一个 Activity 时调用 onPause(),当 Activity 处于完成模式时调用 onStop() 方法。”这对我来说似乎是错误的,当我开始另一个活动时,两者都在调用,我只是开始了活动并没有完成它,但仍然 onPauseonStop 方法正在调用。

Doc 说“当您的活动时间不长时,它会调用 onStop”我很好,它会调用 onStop

当您的 Activity 不再可见并被另一个 Activity 隐藏但仍然onPause 也在调用但为什么??

【问题讨论】:

  • @Raghunandan 我读过,无法完全理解
  • 也可以在这里查看:Activity Lifecycle。简单地说:onPause() 在活动被部分隐藏时被调用(例如AlertDialog 显示)。 onStop() 在活动完全隐藏时调用(例如转移到另一个活动)。我有点同意链接的答案并不完全正确。
  • @Williams 文档具有可见生命周期和前台生命周期。这应该足以理解活动的生命周期。您还可以记录信息覆盖活动生命周期方法来检查自己,而不是查看其他帖子
  • @Raghunandan 如果我已经明白了,否则我不会在这里问

标签: android android-activity


【解决方案1】:

为简单起见,这里是来自 android developers documentation 的小信息。

Activity Lifecycle

Activities in the system are managed as an activity stack. When a new activity is
started, it is placed on the top of the stack and becomes the running activity -- 
the previous activity always remains below it in the stack, and will not come to the
foreground again until the new activity exits.

An activity has essentially four states:

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.

请参考this生命周期演示应用。这对理解 Activity 生命周期非常有帮助。

正如 Raghunandan 所建议的,你必须阅读this

添加:

例如:当用户从 Activity A 遍历到 Activity B(全屏非透明)时,发生以下事情

  1. Activity A 的状态变为暂停状态(在 Activity A 上调用 onPause)
  2. 然后活动 A 的状态从暂停状态变为停止状态(调用 onStop 活动 A) 。 onStop 被调用是因为 Activity B 是全屏的 不透明的活动。 (如果 Activity B 不是全尺寸或透明的,则不会在 Activity A 上调用 onStop)

onPause 保证在 Activity A 上被调用(无论 Activity B 是非全尺寸透明还是全尺寸)。 onStop 只会在 Activity A 被全尺寸的 Activity B 完全覆盖时调用。

因此,在 onPause 中,您可以根据需要保存 Activity 的状态或其他一些有用信息。

祝你好运:)

【讨论】:

  • 如果一个活动失去焦点但仍然可见(也就是说,一个新的非全尺寸或透明活动的焦点在你的活动之上),它会被暂停。暂停的活动是完全活动的(它维护所有状态和成员信息并保持连接到窗口管理器),但可以在内存极低的情况下被系统杀死。 ---- 在这里,当一个新的全尺寸活动专注于您的活动时,您以前的活动如何仍然可见??
  • @Williams 如果全尺寸活动 B 具有焦点并且位于活动 A 的顶部,则活动 A 不可见并且处于停止状态。活动 B 将可见并正在运行。请参考 Android 团队的这个developer.android.com/training/basics/activity-lifecycle/… 演示项目。它真的会帮助你理解生命周期回调。希望我回答了你的问题。
  • 我已经有一个例子了。当您的活动不再可见和被另一个活动隐藏但仍然 onPause 正在调用但为什么??
  • @Williams 这就是 Activity 生命周期的工作方式。当一个 Activity 被其他 Activity 覆盖(无论它是非全尺寸透明还是全尺寸)时,保证会调用 onPause。 onStop 只会在 Activity 被其他全尺寸 Activity 覆盖时调用。因此,在 onPause 中,您可以根据需要保存 Activity 的状态或其他一些有用信息。
  • @Williams 你也可以查看这个帖子stackoverflow.com/questions/9266417/…
猜你喜欢
  • 2017-03-17
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多