DialogFragment.getDialog returns null

前端 未结 5 1331
感动是毒
感动是毒 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

    0 讨论(0)
  • 2020-12-02 17:09

    There is 2 way to show DialogFragment:

      void showDialog() { 
        // Create the fragment and show it as a dialog. 
        DialogFragment newFragment = MyDialogFragment.newInstance(); 
        newFragment.show(getFragmentManager(), "dialog");
    } 
    

    And

     FragmentTransaction ft = getFragmentManager().beginTransaction();
    DialogFragment newFragment = MyDialogFragment.newInstance(); 
    ft.add(R.id.embedded, newFragment);
    ft.commit(); 
    

    You can only get a nonNull dialog when using the first way.

    0 讨论(0)
  • 2020-12-02 17:10

    You're calling getDialog() too early in the DialogFragment's life cycle.

    getDialog() simply returns the private variable mDialog from the DialogFragment.

    When a DialogFragment is instantiated mDialog is null, and then it gets set when onCreateDialog is fired inside getLayoutInflater(Bundle savedInstanceState), so you have to call getDialog after onCreateDialog.

    For example, the order of some common methods called is onCreate, onCreateDialog, and onCreateView, onStart. So, you can call getDialog and have it return something in onCreateView or onStart, but not in onCreate or onCreateDialog.

    Even though onStart is called called when the Fragment is visible to the user, adjusting the layout of the fragment at that point looks fine.... for example setting the width and height using getDialog().getWindow().setLayout(..., ...); doesn't make the fragment appear to change size, but just appears to have the newly set size.

    0 讨论(0)
  • 2020-12-02 17:13

    Try calling executePendingTransactions() from the available FragmentManager.

        dialogFragment = new DialogFragment();
            ...
        dialogFragment.show(mFragmentActivity.getSupportFragmentManager(), "Dialog");
        mFragmentActivity.getSupportFragmentManager().executePendingTransactions();
    
        Dialog d = dialogFragment.getDialog()
            ...
    
    0 讨论(0)
  • 2020-12-02 17:20
    public class Dialog extends DialogFragment {
    
    private DialogListener dialogListener;
    
    
    public void setDialogListener(DialogListener dialogListener) {
        this.dialogListener = dialogListener;
    }
    
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.layout_dialog, null);
        return view;
    }
    
    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (null != dialogListener) {
            dialogListener.onDismiss();
        }
    }
    
    public interface DialogListener {
        void onDismiss();
    }
    

    }

    in Activity ...

      Dialog  dialog= new Dialog();
            dialog.setDialogListener(new Dialog.DialogListener() {
                @Override
                public void onDismiss() {
                    Foo()..
                }
            });
    
    0 讨论(0)
提交回复
热议问题