I'm trying to discern, during onStart(), whether my Activity was started from the home screen or "back" from another activity.
getIntent().hasCategory("android.intent.category.LAUNCHER")
does not work, as the activity's intent stays the same.
I want to display a dialog box when the main activity starts, but I don't want it popping up every time a user goes back to the main activity after visiting another.
Is开发者_JAVA百科 there any way to accomplish this?
Thanks for any help! -Chase
Yes it is possible to launch a dialog box only when the main activity starts the first time, but I save state on a soft kill. So if you save state in onSaveInstanceState on a soft kill you can look for a null bundle in onCreate. If the bundle is null, then this is the first launch. If the bundle is not null, then you are returning from a soft kill. If you save state in onRetainNonConfigurationState then the code looks like:
// RESTORE STATE HERE
// Save state in onStop (prefs) and onRetainNonConfigurationInstance (ConfuseTextState)
state= (ConfuseTextState)getLastNonConfigurationInstance();
if (state != null) { // at least second pass, get non view state from onRetainNonConfigurationInstance
try {
this.isShowCharCount= state.isShowCharCount;
this.timeExpire= state.timeExpire;
this.timeoutType= state.timeoutType;
this.isValidKey= state.isValidKey;
this.password= state.password;
this.isAutoLaunch= state.isAutoLaunch;
//Log.d(TAG,"restoredStateFromLastConfiguration");
}
catch(Exception e){
Log.d(Utilities.TAG,"FailedToRestoreState",e);
}
}
else { // first pass, get saved state from preferences on first pass if they exist
// Restore preferences (8) on hard kill when USER hit back and killed us
SharedPreferences prefs = getPreferences(MODE_PRIVATE); // singleton
if (prefs != null){...
} // else state is from xml files and default instance values
// SUPPORT EASY LAUNCH
if (isAutoLaunch){ // launch on first show only
this.showDialog(DIALOG_EASY_LAUNCH); //<== SHOW YOUR ALERT HERE!
}
}
精彩评论