屏幕旋转后恢复对话框

Restore dialog after screen rotation

我有一个对话框(来自这个 lib 的 material 对话框)和 2 个编辑文本。在我旋转设备 activity 之后,对话框也会被破坏。我知道 edittext 可以自动保存输入文本,但我怎样才能禁止对话框被破坏?

 MaterialDialog dialog = new MaterialDialog.Builder(context).customView(R.layout.dialog_edit_point_info, false)
            .positiveText(R.string.ok).negativeText(R.string.cancel)
            .positiveColorRes(R.color.dark_blue).negativeColorRes(R.color.black)
            .iconRes(R.drawable.ic_marker_location)
            .autoDismiss(false)
            .title(R.string.edit_title_dialog).callback(callback).build;

将对话框放入 DialogFragment 中:

public static class MyAlertDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    MaterialDialog dialog = new MaterialDialog.Builder(context).customView(R.layout.dialog_edit_point_info, false)
        .positiveText(R.string.ok).negativeText(R.string.cancel)
        .positiveColorRes(R.color.dark_blue).negativeColorRes(R.color.black)
        .iconRes(R.drawable.ic_marker_location)
        .autoDismiss(false)
        .title(R.string.edit_title_dialog).callback(callback).build();

}
}

调用对话框:

void showDialog() {
    DialogFragment newFragment = new MyAlertDialogFragment();
    newFragment.show(getFragmentManager(), "dialog");
}

您可以在此处找到更多信息:http://developer.android.com/reference/android/app/DialogFragment.html

如果你使用Support-Libs使用相应的支持类:http://developer.android.com/reference/android/support/v4/app/DialogFragment.html

用这个 lib 代替你的。它是广泛可定制的,将对话框显示为 DialogFragment 并且可以显示从其他对话框调用的对话框。