DialogFragment.getDialog returns null

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

Basically I want to change the text in the layout from the FragmentActivity, which creates and displays a dialog box.

+51
android
Dec 10 '11 at 11:19
source share
5 answers

You call getDialog() too early in the DialogFragmen'ts life cycle.

getDialog() simply returns the private mDialog variable from the Dialog dialog box.

When creating an instance of DialogFragment, mDialog is null, and then it is set when onCreateDialog run inside getLayoutInflater(Bundle savedInstanceState) , so you need to call getDialog after onCreateDialog .

For example, the order of some common methods is called onCreate , onCreateDialog and onCreateView , onStart . That way you can call getDialog and return it to onCreateView or onStart , but not to onCreate or onCreateDialog .

The eventough onStart is called when the Fragment is visible to the user , setting the fragment layout at this point looks great ... for example, setting the width and height using getDialog().getWindow().setLayout(..., ...); does not change the size of the fragment, but just a new set size appears.

+72
Feb 22 '12 at 19:43
source share

Try calling executePendingTransactions () from an available FragmentManager.

  dialogFragment = new DialogFragment(); ... dialogFragment.show(mFragmentActivity.getSupportFragmentManager(), "Dialog"); mFragmentActivity.getSupportFragmentManager().executePendingTransactions(); Dialog d = dialogFragment.getDialog() ... 
+53
Feb 14 '13 at 9:59
source share

There are 2 ways to display 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(); 

At first use, you may get a nonNull dialog.

+7
Nov 09 '16 at 3:48
source share
 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 action ...

  Dialog dialog= new Dialog(); dialog.setDialogListener(new Dialog.DialogListener() { @Override public void onDismiss() { Foo().. } }); 
+1
Aug 28 '17 at 10:22
source share

One of the reasons why getDialog() can return null after the dialog has been created and saved correctly in mDialog is the random call to DialogFragment dismiss() in DialogFragment .

When mDialog dismiss() , it resets the mDialog field to null so that subsequent calls to getDialog() return null instead of the previously created dialog.

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

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

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

0
May 8 '19 at 4:49
source share



All Articles