Android 旋转屏幕时,AlertDialog将消失
在我的活动中,我有一个AlertDialog,当按下按钮时显示。我注意到,当屏幕旋转时,应用程序崩溃了。为了解决这个问题,我在Android 旋转屏幕时,AlertDialog将消失,android,android-alertdialog,Android,Android Alertdialog,在我的活动中,我有一个AlertDialog,当按下按钮时显示。我注意到,当屏幕旋转时,应用程序崩溃了。为了解决这个问题,我在onCreateDialog()方法中添加了这行代码:setRetainInstance(true) 之后,活动不再崩溃,但AlertDialog消失 我在另一个活动中也做了同样的事情,默认情况下,当某个条件为真时会显示一个对话框,并且在屏幕旋转时成功显示了该对话框,因此,我担心我的问题在于,单击按钮时会调用该对话框 我还没有尝试过这样的清单解决方法: <activ
onCreateDialog()方法中添加了这行代码:setRetainInstance(true)代码>
之后,活动不再崩溃,但AlertDialog消失
我在另一个活动中也做了同样的事情,默认情况下,当某个条件为真时会显示一个对话框,并且在屏幕旋转时成功显示了该对话框,因此,我担心我的问题在于,单击按钮时会调用该对话框
我还没有尝试过这样的清单解决方法:
<activity
android:name="YourActivityName"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:windowSoftInputMode="adjustPan">
</activity>
我找到了一个非常丑陋的方法来解决这个问题。基本上,当我按下按钮时,我将静态变量设置为true
然后,在onCreate中,我检查该变量,如果它为真,我将重新创建对话框
我绝对相信一定有更正确的方法可以做到这一点,但我真的找不到。正如您所知,当屏幕方向改变时,Android系统会破坏活动。正确的处理方法是通过活动管理对话框
protected Dialog onCreateDialog(int id) {
// create and return your dialog instance here
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle(title)
.setIcon(R.drawable.indicator_input_error)
.setMessage(message)
.create();
dialog.setButton(
DialogInterface.BUTTON_POSITIVE,
context.getString(R.string.OK),
(DialogInterface.OnClickListener) null);
return dialog;
}
protected void onPrepareDialog(int id, Dialog dialog) {
// You dialog initialization code here
}
您可以使用
showDialog(yourDialogID);
为什么不在屏幕旋转前在bundle上传递一个布尔变量,然后在旋转后从中的数据再次显示对话框bundle@Der傀儡,不完全是,我使用的是DialogFragment。这样我就不用DialogFragment了,应该避免使用,阅读安卓文档。您在示例中从何处找到了对话框片段
?正是我的观点,安卓文档建议使用对话框片段
,而不是直接实例化对话框
。这一点现在被弃用,取而代之的是对话框片段
showDialog(yourDialogID);