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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With