Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: overriding onPause and onResume - proper way

When overriding the onPause() and the onResume() methods of the activity, where is the proper location to call the super.onPause() and super.onResume()? At the beginning of the method or at the end?

like image 620
Genry Avatar asked Oct 19 '12 12:10

Genry


People also ask

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 difference between onStop and onPause?

onStop() is called when the activity is has already lost the focus and it is no longer in the screen. But onPause() is called when the activity is still in the screen, once the method execution is completed then the activity loses focus.

How do I use onPause on Android?

you can just override onPause() in your activity say activtyA when you are navigating to another activity say activityB and override onResume() when u come back to the activityA.

What is the use of onPause () method?

The onPause() function is called when the application enters the paused state by any action on the activity when it is in the resumed state. The callback is called as a result of activity going to the background(activity still may be visible to the user in multi-screen mode).


2 Answers

From http://developer.android.com/guide/components/activities.html#ImplementingLifecycleCallbacks:

Your implementation of these lifecycle methods must always call the superclass implementation before doing any work, as shown in the examples above.

So, for lifecycle callbacks, like onPause() and onResume(), we should do super.onPause() or super.onResume() at the very beginning. For other methods, it all depends on the semantics of the super class.

like image 91
espinchi Avatar answered Oct 09 '22 08:10

espinchi


Update: This is the accepted answer and it contains a nice amount of good information, including a useful diagram, pulled together into one place. However, it appears to be incorrect, at least according to the current Android documentation which, as the poster points out, is the ultimate source for information on the SDK. Possibly the documentation was clarified after this answer was posted. But, in any case, don't stop reading with this answer, check out espinchi's answer below. It has the documentation on its side.


Placement of the super methods depends only on your preference. It would only matter if those methods were taking parameters OR if you were doing some concurrent work. For example if you do this:

    @Override
    protected void onPause() {
        try {
            someOtherThread.join();
        } catch (InterruptedException e) {
            LOG.e(e);
        }
        super.onPause();
    }

it might block the thread and prevent super from being called.

I suggest that you should read all documentation available because they will help you much. For example this is what you can find in the onPause javadoc. I bolded out the important parts:

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

When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState(Bundle) is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)

After receiving this call you will usually receive a following call to onStop() (after the next activity has been resumed and displayed), however in some cases there will be a direct call back to onResume() without going through the stopped state.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

I do recommend this flowchart for you it will help your development tremendously:

Android Activity Flowchart

like image 39
Adam Arold Avatar answered Oct 09 '22 09:10

Adam Arold