问题
When an application's process is killed, its activity stack is saved. Then when the application is restarted, all my activities resume and run into null pointers. Rather than modify every activity to accommodate this event, I would rather just have my application start with the base activity and not try to re-create the activity stack.
Is this possible?
I know about Intent.FLAG_ACTIVITY_CLEAR_TOP, but as I understand that will only kill activities after they have been re-created.
EDIT: is clearTaskOnLaunch what I want? I've added it to my default activity, but am seeing no effect. But this will kill my activities even if I just minimize the application, wont it? I'd rather only clear the stack if the entire process is rebooting.
EDIT 2: No, it's not what I want- an Android engineer gave a thorough response to some questions on how to use ClearTaskOnLaunch: http://groups.google.com/group/android-developers/browse_thread/thread/da024bcaf4e1960f/ab1e2b35c360cb65?pli=1
回答1:
The only solution I was able to find was to check a global static variable in every instance of onCreate() and finish if that variable had been reset to null, indicating the task had been restarted. I close all activities down to my root activity and start over.
Soon I hope to have my app at a point where it can save needed values in onPause(), but 'til then this is the only reliable way I know to work with lost initialization...
回答2:
I use this piece of code:
public class NoRestoreActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Shoul be always NULL when created the first time, just in case...
if (savedInstanceState != null && savedInstanceState.getInt("SavedInstance") > 0) {
// ----- Your prefferred way to kill an application ----
try {
this.finishActivity(0);
} catch (Exception ee) {
}
try {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
} catch (Exception eeee) {
}
return;
}
super.onCreate(savedInstanceState);
}
@Override
protected void onSaveInstanceState (Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("SavedInstance",1);
}
}
回答3:
Is this something one should take into account when dealing with native Android tools, or is this an issue brought up by third party task killers? At least on emulator using "force stop" seems to reset activity stack - which is something I've expected to happen always when application dies.
来源:https://stackoverflow.com/questions/5423571/prevent-activity-stack-from-being-restored