Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why "onPause" is not called in following situation?

By the document, "onPause" is called, when:

when the system is about to start resuming a previous activity.

Compared to "onStop", the difference is:

Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one.

But when I long-press HW-Home key and the "recent apps" shows up, the "onPause" is not called.

At this moment, I can no long able to interact with the original activity, but it is still visible.

I am confused by this situation. Please help to explain.

Thank you very much. BR, Henry

like image 392
Henry Avatar asked Feb 10 '12 10:02

Henry


People also ask

Is onPause always called?

Even if your activity slips into the background when another activity starts or when the screen switches off, onPause() is always called even if the other two methods aren't called. So even if activity ceases, onPause() will be called and your thread will be killed.

When onPause method is called when?

Unfortunately the onPause() method is called immediately after onCreate.

Is onPause always called before onStop?

onPause() is always called. This is guaranteed. If you need to save any state in your activity you need to save it in onPause() . onStop() may be called after onPause() , or it may not.

Why onPause is called?

During this time, the activity is in front of all other activities on screen and has user input focus. An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a dialog appears.


3 Answers

This actually happens because when the Home key is long pressed, there is no activity being launched. The onPause/onStop will only be called if you select one of the apps present in the "Recent Apps" list.

The docs of onPause() are pretty clear:

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.

like image 169
MobileCushion Avatar answered Oct 05 '22 19:10

MobileCushion


In a lot of modern phones, the recent apps list is an activity, and does trigger onPause. I've tested this on a couple of Samsung and LG phones, and the packages are com.android.systemui and com.lge.launcher2 respectively.

That said, it seems like you can't rely on recieving an onPause when you open the recent applications screen. I'm curious to see how many device don't give you the onPause...

like image 21
Anubian Noob Avatar answered Oct 05 '22 19:10

Anubian Noob


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 logcat.press back button and then check.after again start application and

Press call button then check logcat now press Back button and again check logcat. you can easily understand life cycle of Activity.

like image 38
arpit Avatar answered Oct 05 '22 19:10

arpit