Android Activites 的 onPause() 和 onStop() 有什么区别?
从这里的android文档 http://developer.android.com/reference/android/app /Activity.html, 它说“Activity 进入前台”将调用 onPause()
,“Activity 不再可见”将调用 onStop()
。
“Activity 进入前台”与“Activity 不再可见”不是一样吗? 你能告诉我它们之间有什么区别吗?
From android doc here http://developer.android.com/reference/android/app/Activity.html,
it said 'Activity comes into foreground' will call onPause()
, and 'Activity is no longer visible' will call onStop()
.
Isn't 'Activity comes into foreground' same as 'Activity is no longer visible'?
Can you please tell me what is the difference between them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不,如果某个活动进入前台,并不一定意味着另一个活动完全不可见。考虑以下情况:
这里我们同时看到这两个活动。第一个具有字段的活动被另一个活动遮盖,用户无法再与其交互。然而,它及其造成的所有后果仍然是可见的。
这就留下了一个问题,哪些活动被认为是完全不透明并覆盖整个屏幕,哪些不是。该决定基于包含活动的窗口。如果窗口具有标志
windowIsFloating
或windowIsTranslucent
,则认为该 Activity 不会使底层内容不可见,否则会导致onStop ()
被调用。相关代码可以在com.android.server.am.ActivityRecord
中找到:No, if some activity comes into foreground, that doesn't necessarily mean that the other activity is completely invisible. Consider the following case:
Here we see both activities at the same time. The first activity with the fields is obscured by another activity, and the user can no longer interact with it. However, it is still visible with all the resulting consequences.
That leaves a question which activity is considered fully opaque and covering the whole screen and which isn't. This decision is based on the window containing the activity. If the window has a flag
windowIsFloating
orwindowIsTranslucent
, then it is considered that the activity doesn't make the underlying stuff invisible, otherwise it does and will causeonStop()
to be called. The relevant code can be found incom.android.server.am.ActivityRecord
:如果您仍然可以看到它的任何部分(
Activity
来到前台要么不占据整个屏幕,要么有点透明),则将调用onPause()
。如果您看不到它的任何部分,onStop()
将被调用。例如,对话框**可能不会覆盖整个先前的
Activity
,这将是调用onPause()
的时间。**我在这里指的不是 Android 对话框,而是弹出并仅遮盖部分用户屏幕的概念。添加此注释是为了根据下面 @GMsoF 的评论进行澄清
If you can still see any part of it (
Activity
coming to foreground either doesn't occupy the whole screen, or it is somewhat transparent),onPause()
will be called. If you cannot see any part of it,onStop()
will be called.A dialog**, for example, may not cover the entire previous
Activity
, and this would be a time foronPause()
to be called.**I am not referring to an Android Dialog here, rather a conceptual idea of something that pops up and only obscures part of the user screen. This note was added to clarify based on a comment from @GMsoF below
实际上,人们应该考虑“onPause()”和“onPause() + onStop()”之间的区别。
每当发生一些新活动时并占据屏幕的部分空间。因此您之前运行的活动在某种程度上仍然可见。在这种情况下,先前运行的活动不会被推送到 Back Stack。 所以,这里只调用了onPause()方法。
另一方面,如果出现一些新的 Activity 并占据全屏,那么您之前运行的 Activity 就会消失。在这种情况下,您之前运行的活动将移至 Back Stack。 这里调用了 onPause() + onStop()。
总结 -
onPause()- 屏幕部分被其他新活动覆盖。该 Activity 不会移至 Back Stack。
onPause() + onStop()- 屏幕被其他新活动完全覆盖。该 Activity 被移至 Back Stack。
了解更多有关返回堆栈的信息。
Practically, one should consider the difference between “onPause()” and “onPause() + onStop()”.
Whenever some new activity occurs and occupies some partial space of the Screen. So your previously running activity is still visible to some extent. In this Case, the previously running activity is not pushed to Back Stack. So, here only onPause() method is called.
On other hands, if some new Activity occurs and occupies the full screen so that your previously running activity is disappeared. In this Case, your previously running activity is moved to Back Stack. Here, onPause() + onStop() are called.
To Summaries-
onPause()- Screen is partially covered by other new activity. The Activity is not moved to Back Stack.
onPause() + onStop()- Screen is fully covered by other new activity. The Activity is moved to Back Stack.
Know more about- Back Stack.
位于前台意味着该活动具有输入焦点。例如,一个活动可能是可见的,但被具有焦点的对话框部分遮挡。在这种情况下,将调用
onPause()
,但不会调用onStop()
。当对话框消失时,将调用 Activity 的onResume()
方法(但不会调用onStart()
)。Being in the foreground means that the activity has input focus. For instance, an activity can be visible but partially obscured by a dialog that has focus. In that case,
onPause()
will be called, but notonStop()
. When the dialog goes away, the activity'sonResume()
method will be called (but notonStart()
).每当新的 ACTIVITY 启动时,前一个 Activity 的
onPause
在任何情况下都会被调用。实际上会有两种情况:
1- 之前的 Activity 的一部分可见或者新的 Activity 是透明的:仅调用
onPause
。2- 之前的活动完全被新活动覆盖:
onPause
和onStop
都会被调用---- 需要说明一些注意事项:
注意 1:如果对话框开始于活动顶部
onPause
或onStop
不会被调用。注意2:如果它的主题设置为对话框的活动,则行为将与普通活动一样。
注 3:显然是一个类似于权限对话框的系统对话框,因为棉花糖会导致
onPause
。whenever a new ACTIVITY starts the previous activity's
onPause
will be defiantly called in any circumstances.actually there will be two circumstances:
1- a part of previous activity is visible or the new activity is transparent: only
onPause
will be called.2- previous activity is completely covered by new activity: both
onPause
andonStop
will be called----Good to state some notes:
NOTE 1: if a dialog starts on top of an activity NONE of
onPause
oronStop
will be called.NOTE 2: if its an Activity whose theme is set to a dialog, the behavior will be just like a normal activity.
NOTE 3: apparently a system dialog like permission dialog since marshmallow will cause
onPause
.简而言之:
当显示另一个活动时,将调用先前活动生命周期方法的
onStop()
。当 Dialogue 位于 Activity 顶部时,将调用onPause()
。注意:活动是那些填满整个屏幕的组件。
注意:对话不是活动,因为它们没有完全填满屏幕。
In concise words:
onStop()
of previous activity life-cycle method is invoked when another activity is shown. When you have Dialogue on the top of activity, thereonPause()
is invoked.Note: Activities are those components which fill your entire screen.
Note: Dialogues are not Activity as they don't completely fill the screen.
我在 onPause 和 onStop 方法方面遇到了很多问题,因此我将澄清我遇到的三个场景 -
1. 当您单击最近使用的应用程序按钮时,不会调用任何生命周期方法,但会调用 onWindowFocusChanged(boolean hasFocus) 并将 hasFocus 值传递为 false。在 Android 5 之前的版本中,onPause 方法用于在按下最近使用的应用程序按钮时被调用。
2. 当您的活动上出现类似弹出的活动时,如Malcolm所述, onPause 按钮被调用。如果调用占据整个屏幕的新 Activity,则会在前一个 Activity 上调用 onStop。 Android 权限对话框< /a> 也会导致您的 Activity 调用 onPause。
3. 如果屏幕在您的活动上超时,则会调用 onPause。一段时间后,如果您不打开屏幕,则会调用 onStop。
ateiob 还提到了一件重要的事情来完成答案
希望有帮助。
I faced many problems with onPause and onStop methods and hence I will clear three scenarios that I came across-
1. When you click on the recent app button, no life cycle method is called but the onWindowFocusChanged(boolean hasFocus) is called with hasFocus value passed as false. In android version before 5 , onPause method used to get called, on pressing the recent app button.
2. When a pop up like activity appear over your activity, as mentioned by Malcolm, the onPause button is called. If new activity that takes up the whole screen is called, then onStop is called on previous activity. Android permission dialog also cause your activity to call onPause.
3. If screen times out on your activity, then onPause is called. After sometime if you will not open the screen then onStop will be called.
Also one important thing mentioned by the ateiob that completes the answer
Hope it helps.
是的,我尝试理解,我可以在下面解释这一点:
有 2 个活动:ActivityA 和 ActivityA。 ActivityB
这是活动 B。按照我在代码中的评论
我希望这很清楚
Yeap, I try to understand and I can explain this below:
There are 2 activities: ActivityA & ActivityB
Here is activity B. Follow my comment in code
I hope this is clearly