Rachit Rachit - 1 year ago 111
Android Question

Android: Scenario where onPause is called but not onStop?

I'm trying to understand the difference between onPause and onStop.

I've read all the different forums but I still am not clear about the difference. I've created a simple app to try and test when which method gets called. For that I've simply placed loggers in each method.

From my trials -

  1. Popups do not call either method

  2. Switching to another activity calls both methods

  3. Pulling down the notification bar calls neither method

I've only seen either both methods being called in quick succession or neither getting called at all.
I'm trying to find scenarios where onPause gets called but onStop doesn't.

The purpose is to understand whether defining onPause is even required. If the scenarios in which only onPause gets called are so rare, it doesn't even make sense to write separate code for onPause. Shouldn't writing onStop be sufficient?

public class LifecycleActivity extends ActionBarActivity {

protected void onDestroy() {
Log.d("Rachit", "In Destroy Method");

protected void onCreate(Bundle savedInstanceState) {
Log.d("Rachit", "In Create Method");

protected void onStart() {
Log.d("Rachit", "In Start Method");

protected void onResume() {
Log.d("Rachit", "In Resume Method");

protected void onPause() {
Log.d("Rachit", "In Pause Method");

protected void onRestart() {
Log.d("Rachit", "In Restart Method");

protected void onStop() {
Log.d("Rachit", "In Stop Method");

Answer Source

I figured this out a while later, but forgot to post my answer here.

The instance where I noticed that onPause() was called, without an immediate subsequent call to onStop() was when I got a notification from a different application when a different app was open on my phone.

For example, say you have Facebook running on your phone currently. If you receive a notification from WhatsApp (in the pop-up dialog box), your Facebook activity will be paused. If you close the WhatsApp pop-up during this time, the Facebook Activity will get Resumed. On the other hand, if you open WhatsApp from the pop-up message, your Facebook Activity will be stopped.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download