我正在尝试使用来自Docs的Alertdialog。但有一个问题。如果我重新旋转手机警报对话框,则会出现两个警报对话框。如何防止重新创建alertDialog。
这是我的代码:
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
这是显示方法:
public void confirmFireMissiles() {
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}
答案 0 :(得分:2)
检查是否设置了savedInstanceState
:
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (savedInstanceState == null) {
// create new dialog
}
}
答案 1 :(得分:-1)
添加此
清单文件中android:configChanges="orientation|screenSize"
标记内的 Activity
属性。