我在onStart()期间试图辨别我的Activity是从主屏幕启动还是从其他活动“退回”。
getIntent().hasCategory("android.intent.category.LAUNCHER")
不起作用,因为活动的意图保持不变。
我想在主要活动开始时显示一个对话框,但我不希望每次用户在访问另一个活动后返回主活动时都会弹出。
有没有办法实现这个目标?
感谢您的帮助! -Chase
答案 0 :(得分:1)
是的,只有在主活动第一次启动时才可以启动对话框,但我在软杀死时保存状态。因此,如果在软杀死中将状态保存在onSaveInstanceState中,则可以在onCreate中查找空包。如果bundle为null,那么这是第一次启动。如果bundle不为null,那么你将从soft kill返回。如果在onRetainNonConfigurationState中保存状态,则代码如下所示:
// 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!
}
}