10

WAY1:

@Override
protected void onPause() {
    super.onPause();
    // do something urgent
    doSomething();
}

WAY2:

@Override
protected void onPause() {
    // do something urgent
    doSomething();
    super.onPause();
}

The difference is the calling sequence of doSomething() and super.onPause(). When I use the WAY1, if the doSomething() cost too much, i will get the error: W/ActivityManager( 4347): Activity pause timeout for ActivityRecord .

Do i avoid the pause timeout error if i use WAY2 ?

I checked the AOSP , but it's too hard for me to understand the invoking procedure of Activity.

Jerikc XIONG
  • 3,517
  • 5
  • 42
  • 71
  • According to http://developer.android.com/intl/es/training/basics/activity-lifecycle/pausing.html you must always call the superclass method first – antonio Mar 09 '16 at 12:27
  • @antonio Yes. I saw that. Do you know the reason ? – Jerikc XIONG Mar 09 '16 at 12:32
  • 1
    http://stackoverflow.com/a/9626268/1320616 and http://stackoverflow.com/a/18874519/1320616 will correctly explain you everything you need to know – Ankit Aggarwal Mar 09 '16 at 12:53

2 Answers2

10

The documentation says that

You should keep the amount of operations done in the onPause() method relatively simple in order to allow for a speedy transition to the user's next destination if your activity is actually being stopped.

If you take a look at the ActivityStack class that is used for "State and management of a single stack of activities.", it defines

// How long we wait until giving up on the last activity to pause.  This
// is short because it directly impacts the responsiveness of starting the
// next activity.
static final int PAUSE_TIMEOUT = 500;

So, if the operations performed by onPause() exceed this timeout, you will receive the message Activity pause timeout.

As this timeout is set for the pause of the Activity and onPause() is just a callback method that allows you to perform operations when an Activity pauses, changing the order (WAY1 or WAY2) will not affect the timeout (it will be triggered both in WAY1 and WAY2). To prove it, both these codes print the Activity pause timeout message:

// WAY1
@Override
protected void onPause() {
    super.onPause();

    try {
        Thread.sleep(501); // Don't do this!!! Only for testing purposes!!!
    }catch (Exception e) {
    }
}

// WAY2
@Override
protected void onPause() {
    try {
        Thread.sleep(501); // Don't do this!!! Only for testing purposes!!!
    }catch (Exception e) {
    }

    super.onPause();
}

As a side note, as I say in my comments, the documentation says that you must always call the superclass method first, but as @ankit aggarwal states, What is the correct order of calling superclass methods in onPause, onStop and onDestroy methods? and Why? is a very good answer explaining if WAY2 is better than WAY1 and why.

Community
  • 1
  • 1
antonio
  • 18,044
  • 4
  • 45
  • 61
0

Do i avoid the pause timeout error if i use WAY2 ?

No you wouldn't.

What do you do in doSomething(); method? cleaning shouldn't take that much of time.
If you sending data over internet or save data to DB? Move this code to a Service/IntentService or a Thread.


Call super.xxx() first or myMethod() first?

For me I prefer to:

  • Calling super.xxx() FIRST in starting methods onCreate() / onStart() / onResume()
  • Calling super.xxx() LAST in stopping methods onPause() / onStop() / onDestroy()

With this approach you maintain the call stack, Android initialized everything in the Activity (for example) before you use it. and you cleaned your variables before Android start cleaning the Activity

JafarKhQ
  • 8,676
  • 3
  • 35
  • 45
  • I cleaning up camera resource in `doSometing`. In some devices, the `Camera#release()` cost too much time and cause the `W/ActivityManager( 4347): Activity pause timeout for ActivityRecord`. I want to know when start recording for the `timeout`. – Jerikc XIONG Mar 09 '16 at 13:08
  • @JerikcXIONG replace`doSomething();` with `Camera#release()` only and try again. to see if this because of other code inside `doSomething();` – JafarKhQ Mar 09 '16 at 13:25