DialogFragment.getDialog returns null

前端 未结 5 1347
感动是毒
感动是毒 2020-12-02 16:30

I am trying to get the Dialog I have created with an extended DialogFragment using DialogFragment.getDialog() but it returns null.

5条回答
  •  眼角桃花
    2020-12-02 16:59

    One reason for why getDialog() might return null after the dialog has been constructed and properly stored in mDialog is an accidental invocation of dismiss() on the DialogFragment.

    When dismiss() is called, it will reset the mDialog field to null so that subsequent invocations of getDialog() will return null instead of the previously constructed dialog.

    In my case, dismiss() was called to handle an error situation / side-case in the DialogFragment's onActivityCreated() method. Subsequently trying to use getDialog() from the onResume() method returned null.

    Also refer to the source code of the DialogFragment class, specifically its dismissInternal(boolean allowStateLoss) method:

    https://github.com/aosp-mirror/platform_frameworks_base/blob/pie-platform-release/core/java/android/app/DialogFragment.java

提交回复
热议问题