Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between onPause() and onStop() of Android Activites?

People also ask

What is the difference between onPause and onStop Android?

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.

What is the difference between onPause and onResume?

onPause() gets called when your activity is visible but another activity has the focus. onResume() is called immediately before your activity is about to start interacting with the user. If you need your app to react in some way when your activity is paused, you need to implement these methods.

What is the use of an onStop () function in Android?

The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user.

What is onStop () activity?

Summary: onStop() method is called after onPause() method when activity goes in background. This method can be used to stop Api calls etc.


No, if some activity comes into foreground, that doesn't necessarily mean that the other activity is completely invisible. Consider the following case:

Activity with the theme Theme.Dialog

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

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


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.


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