假设我打开了一个对话框,然后我将手机侧向一边。我希望该对话框保持打开状态。我如何做到这一点?
这是我尝试过的
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 中,就像它不再识别对话框一样?