Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to call super.onPause()?

I am implementing Analytics in my android application, and I would like advice on when to call super.onPause()

if (mAnalyticsSession != null) {
    mAnalyticsSession.close();
    mAnalyticsSession.upload();
}

super.onPause();

What is the effect of calling super.onPause() after doing upload actions vs. before?

In general, when should one call super.onPause()?

like image 769
Android Developer Avatar asked Jan 15 '13 21:01

Android Developer


People also ask

When onResume() method is called?

onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .

When onPause() of activity lifecycle is called?

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. Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.

When onPause is called Android?

onPause. Called when the Activity is still partially visible, but the user is probably navigating away from your Activity entirely (in which case onStop will be called next). For example, when the user taps the Home button, the system calls onPause and onStop in quick succession on your Activity .

When onResume method is called in Android?

onResume() is one of the methods called throughout the activity lifecycle. onResume() is the counterpart to onPause() which is called anytime an activity is hidden from view, e.g. if you start a new activity that hides it. onResume() is called when the activity that was hidden comes back to view on the screen.


1 Answers

You only call super.onPause() in your own Activity.onPause() override.

public class YourActivity extends Activity {

    @Override
    public void onPause() {
        super.onPause();
        // Do your stuff, e.g. save your application state
    }

}

Note that you don't need to override this if you don't need it. If you're going to override it, then do not make slow processes in here or you might get an ANR.

like image 171
m0skit0 Avatar answered Oct 30 '22 22:10

m0skit0