What is the difference between onPause() and onStop() of Android Activites?
Solution 1:
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
or windowIsTranslucent
, then it is considered that the activity doesn't make the underlying stuff invisible, otherwise it does and will cause onStop()
to be called. The relevant code can be found in com.android.server.am.ActivityRecord
:
fullscreen = ent != null && !ent.array.getBoolean(
com.android.internal.R.styleable.Window_windowIsFloating, false)
&& !ent.array.getBoolean(
com.android.internal.R.styleable.Window_windowIsTranslucent, false);
Solution 2:
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 for onPause()
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
Solution 3:
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.
Solution 4:
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 not onStop()
. When the dialog goes away, the activity's onResume()
method will be called (but not onStart()
).
Solution 5:
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
and onStop
will be called
----Good to state some notes:
NOTE 1: if a dialog starts on top of an activity NONE of onPause
or onStop
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
.