Pressing the back button causes onPause to be called, and the app stays paused until it is re-launched by clicking on the icon, at which point, onDestroy gets called, and the main activity continues to shut down.
Simple class to demonstrate. Note, as far as I can tell, this only happens on the Nexus One. I can't reproduce it in the emulator or on my Droid.
package com.vimtips.testshutdown;
import android.app.ActivityGroup;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
public class MainActivity extends ActivityGroup {
private static final String TAG = "MainActivity";
private int counter = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
if(counter-- > 0) return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause called");
}
@Override
public void onDestroy() {
super.onDestroy();
if(isFinishing()) {
Log.d(TAG, "Shutting down");
}
}
}
And here's the log:
I/ActivityManager( 132): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=com.vimtips.testshutdown/.MainActivity }
I/ActivityManager( 132): Displayed activity com.vimtips.testshutdown/.MainActivity: 305 ms (total 305 ms)
D/MainActivity( 1393): onPause called
I/ActivityManager( 132): Displayed activity com.vimtips.testshutdown/.MainActivity: 302 ms (total 302 ms)
D/MainActivity( 1393): Shutting down
This doesn't appear to happen on a normal Activity, just an Activity group, though looking at Android's sourcecode, I can't figure out why. It's causing some seri开发者_运维技巧ous problems with my app.
Anyone know why this would happen?
This is dependent on the launchMode of the app and the intent flags. Currently, on emulator any activity is started with the following flags :
- Intent.FLAG_ACTIVITY_NEW_TASK
- Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
And if there aren't any flags specified then it will restart the activity, which is happening in your case.
精彩评论