說我打開了一個對話框,並將手機側身。我想讓對話保持開放。 我該如何實現這個目標?如何保留對話框的打開狀態onRestoreInstanceState
這是我已經試過
Dialog dialog;
boolean dialogShown = false;
onClick:
dialog = new Dialog(Login.this, R.style.no_title_dialog);
dialog.show();
dialogShown = true;
dialogButton.onClick:
//doStuff
dialog.dismiss();
dialogShown = false;
然後
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(dialogShown) {
outState.putBoolean("dialogShown", true);
}
}
@Override
protected void onRestoreInstanceState(Bundle outState) {
super.onRestoreInstanceState(outState);
outState.getBoolean("dialogShown");
if(dialogShown){
dialog.show();
}
}
但是當我將手機橫置時,我得到一個空指針異常就行:在
dialog.show();
onRestoreInstanceState,就像它不再識別對話框一樣?
我認爲沒有人再使用'Dialog' ...只需正確使用DialogFragment'',一切都將爲您管理,即它將保持打開方向更改等。請參閱http://developer.android.com/reference /android/app/DialogFragment.html and http://android-developers.blogspot.com/2012/05/using-dialogfragments.html –
使用Activity.showDialog()或DialogFragment – hjy