DialogFragment.getDialog returns null

Android

Android Problem Overview


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

Basically I want to alter the text in the layout from the FragmentActivity which creates and shows the DialogFragment.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

Try calling executePendingTransactions() from the available FragmentManager.

	dialogFragment = new DialogFragment();
		...
	dialogFragment.show(mFragmentActivity.getSupportFragmentManager(), "Dialog");
	mFragmentActivity.getSupportFragmentManager().executePendingTransactions();
	
	Dialog d = dialogFragment.getDialog()
		...

Solution 3 - Android

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.

Solution 4 - Android

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()..
            }
        });

Solution 5 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontechi.servicesView Question on Stackoverflow
Solution 1 - AndroidPeter AjtaiView Answer on Stackoverflow
Solution 2 - AndroidGonzaloView Answer on Stackoverflow
Solution 3 - AndroidHai nguyen thanhView Answer on Stackoverflow
Solution 4 - AndroidJiajun ShenView Answer on Stackoverflow
Solution 5 - AndroidBenjaminView Answer on Stackoverflow