【问题标题】:DialogFragment is dismissed right after showDialogFragment 显示后立即关闭
【发布时间】:2014-08-26 19:08:31
【问题描述】:

我创建了一个 DialogFragment,它应该在调用 onActivityResult 后显示。 但是在调用 dialog.show() 之后,Dialog 会无缘无故地自动关闭。

我正在使用 BarcodeScanner 库扫描 QR 码,在 onActivityResult 中我只是保存数据(此时我也尝试显示对话框,但没有成功。)

if ((requestCode == REQUEST_BARCODESCANNER) && (resultCode == RESULT_OK)) {
    mBarcodeScanned = true;
    mBarcodeScanResult = getBarcodeScannerResult(data.getExtras());
}

在 onResume 中我现在正在检查这个变量:

if(mBarcodeScanResult == null && mBarcodeScanned){
    mBarcodeScanned = false;
    showDialog(MyDialogFragment.getInvalidQrCodeDialog(this));
} else if(mBarcodeScanResult != null && mBarcodeScanned){
    showDialog(MyDialogFragment.getSomeDialog(this, v1, v2));
}

在 showDialog() 中我只是调用 show:

dialog.show(getSupportFragmentManager(), MyDialogFragment.class.getSimpleName());

如果扫描了二维码,现在应该会显示对话框。 出于某种原因,在dialog.show() 之后,我在 MyDialogFragment 类中检查了onDismiss(),它也被调用了,但我真的不知道为什么?

MyDialogFragment 正在使用 onCreateDialog 方法,该方法创建 AlertDialogs 以返回。 getSomeDialog()getInvalidQrCodeDialog() 方法只是将 Fragment 实例化。

编辑:MyDialogClass

public class MyDialogFragment extends DialogFragment {

    private static final String BUNDLE_DIALOG_TYPE = "bundle_dialog_type";

    private DialogType mDialogType;

    public enum DialogType{
        QR_CODE_INVALID, SOME_DIALOG
    }

    public static Fragment getInvalidQrCodeDialog(final Context context) {
        Bundle args = new Bundle();
        args.putString(BUNDLE_DIALOG_TYPE, DialogType.QR_CODE_INVALID.name());
        return MyDialogFragment.instantiate(context, MyDialogFragment.class.getName(), args);
    }

    public static Fragment getSomeDialog(final Context context) {
        Bundle args = new Bundle();
        args.putString(BUNDLE_DIALOG_TYPE, DialogType.SOME_DIALOG.name());
        return MyDialogFragment.instantiate(context, MyDialogFragment.class.getName(), args);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleArguments();
    }

    private void handleArguments() {
        final Bundle arguments = getArguments();
        if(arguments != null) {
            mDialogType = DialogType.valueOf(arguments.getString(BUNDLE_DIALOG_TYPE, DialogType.SOME_DIALOG.name()));
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        switch(mDialogType){
            case QR_CODE_INVALID: return DialogHelper.showQRCodeInvalidDialog(getActivity());
            case SOME_DIALOG: return DialogHelper.showSomeDialog(getActivity());
            default: return null;
        }
    }
}

DialogHelper 会做这样的事情:

public static AlertDialog showQRCodeInvalidDialog(final Context context){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(R.string.barcode_invalid);
    builder.setTitle(R.string.barcode_invalid_title);
    builder.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
    return builder.create();
}

【问题讨论】:

  • 给你发mydialogfragment类
  • 如果可以调试,请在onDismiss() 中设置断点,或者如果无法从那里打印堆栈跟踪(请参阅stackoverflow.com/q/1069066/3959454
  • onDismiss() 在我调用 show 后立即调用,我已经调试过了。
  • 使用builder.setPositiveButton(android.R.string.ok, null)
  • 如果我添加一个 200 毫秒的 postDelayed 来显示对话框 - 它可以工作。但这不是一个很酷的解决方案

标签: android android-alertdialog android-dialogfragment dismiss


【解决方案1】:

问题似乎是支持包 DialogFragment。 我只是将它从支持更改为原始的 DialogFragment,一切都按预期工作。

【讨论】:

    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多