Why is "onPause" not called in the following situation?

The document calls "onPause" when:

when the system is about to begin resuming the previous action.

Compared to onStop, the difference is:

Called when an action is no longer displayed to the user, as another action is resumed and covers this one.

But when I press the HW-Home key for a long time and the "latest applications" appear, "onPause" is not called.

At this moment, I cannot interact for a long time with the original activity, but it is still visible.

I am confused by this situation. Please help explain.

Many thanks. BR, Henry

+5
android android-activity lifecycle timing onpause
source share
6 answers

This actually happens because when the Home key is pressed for a long time, the activity does not start. OnPause / onStop will be called only when you select one of the applications present in the Recent Applications list.

The onPause () docs are pretty clear:

Called as part of the life cycle of an activity when an activity occurs in the background but is not yet killed.

+7
source share
public class MainActivity extends Activity { String tag="my result"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v(tag,"I am in oncreate"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.v(tag,"I am in onDestroy"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.v(tag,"I am in onpause"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.v(tag,"I am in onRestart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.v(tag,"I am in onresume"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.v(tag,"I am in onstart"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.v(tag,"I am in onstop"); } } 

Run it and check the logcat.press button back, and then check. After that, run the application again and

Press the call button, then check the logarithm, click the "Back" button and check the logarithm again. you can easily understand the life cycle of an Activity.

+3
source share

In many modern phones, the list of recent applications is activity and launches onPause . I tested this on several Samsung and LG phones, and the com.android.systemui and com.lge.launcher2 respectively.

However, it looks like you cannot rely on getting onPause when you open the latest apps screen. I am interested to know how many devices onPause does not give you ...

+2
source share

@Henry, please try it yourself by testing the application stream. Create an action in which override all methods and the print log in all methods. The thread is onCreate -> onStart / restart -> onResume, and when you press the home button or start any new activity, then it calls onPause -> onStop, and if you end your application, your application destroys / ends where there is confusion.

0
source share

This is what the official docs about onPause () say

Called as part of the life cycle of an activity when an activity occurs in the background, but has not yet been killed. Analogs onResume ().

When action B starts before activity A, this callback will be called on A. B will not be created until A onPause () is returned, so be sure to do nothing long here.

See here for more details.

0
source share

When you open the latest applications, the onStop method is called, the onStop method is called as soon as the activity user interface is not in focus as such

-one
source share

All Articles