Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPause() and onStop() in Activity

Tags:

I am new to Android development and I am still not able to understand the onPause() and onStop() methods in an activity.

In my app, I have a static class that I name Counter, and it keeps the state of variables in memory for the app. My app runs fine in the emulator. What I was trying to test was differential behavior of onPause() versus onStop().

For onPause, I wanted the values stored in the Counter class's members retained, whereas calling onStop() I wanted the counter values reset to zero. So I override onStop() and set the variables inside the counter class to zero. However, in the emulator, I cannot seem to get the app in the Paused state. In the emulator, I open my app, exercise it. Then I hit the home button (not the back button) of the emulator, and launch another app, believing that this would mimic onPause() activity. However, the emulator does not appear to honor this (I am using an armeabi v7a emulator), it seems to always be calling onStop() because my counter values all go back to zero, per my override in onStop(). Is this inherent to the emulator or am I doing something wrong to get my activity into the paused state?

like image 869
Stephan Doliov Avatar asked Jul 08 '12 22:07

Stephan Doliov


People also ask

What is onStop () activity?

The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user. This may happen for the following reasons, the user performing some action that invoked another activity to come to the foreground or the activity finished.

What is the difference between onStop and onPause?

onPause() is called when an activity is about to lose focus. onStop() is called when the activity is has already lost the focus and it is no longer in the screen.

When only OnDestroy is called for an activity without onPause () and onStop ()?

onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.

Why it is required to have onPause () and onStop () callbacks separately?

Why it is required to have onPause () and onStop () callbacks separately? Once your activity is stopped, the system might destroy the instance if it needs to recover system memory.


1 Answers

I'm not sure which emulator you are testing with, but onPause is the one method that is always guaranteed to be called when your Activity loses focus (and I say always because on some devices, specifically those running Android 3.2+, onStop is not always guaranteed to be called before the Activity is destroyed).

A nice way to understand the Activity lifecycle for beginners is to litter your overriden methods with Logs. For example:

public class SampleActivity extends Activity {      /**      * A string constant to use in calls to the "log" methods. Its      * value is often given by the name of the class, as this will       * allow you to easily determine where log methods are coming      * from when you analyze your logcat output.      */     private static final String TAG = "SampleActivity";      /**      * Toggle this boolean constant's value to turn on/off logging      * within the class.       */     private static final boolean VERBOSE = true;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         if (VERBOSE) Log.v(TAG, "+++ ON CREATE +++");     }      @Override     public void onStart() {         super.onStart();         if (VERBOSE) Log.v(TAG, "++ ON START ++");     }     @Override     public void onResume() {         super.onResume();         if (VERBOSE) Log.v(TAG, "+ ON RESUME +");     }      @Override     public void onPause() {         super.onPause();         if (VERBOSE) Log.v(TAG, "- ON PAUSE -");     }      @Override     public void onStop() {         super.onStop();         if (VERBOSE) Log.v(TAG, "-- ON STOP --");     }     @Override     public void onDestroy() {         super.onDestroy();         if (VERBOSE) Log.v(TAG, "- ON DESTROY -");     } } 
like image 164
Alex Lockwood Avatar answered Oct 16 '22 12:10

Alex Lockwood