Reputation: 8533
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
.
Upvotes: 66
Views: 29911
Reputation: 57715
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.
Upvotes: 81
Reputation: 880
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:
Upvotes: 0
Reputation: 211
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()..
}
});
Upvotes: 2
Reputation: 728
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.
Upvotes: 7
Reputation: 3764
Try calling executePendingTransactions() from the available FragmentManager.
dialogFragment = new DialogFragment();
...
dialogFragment.show(mFragmentActivity.getSupportFragmentManager(), "Dialog");
mFragmentActivity.getSupportFragmentManager().executePendingTransactions();
Dialog d = dialogFragment.getDialog()
...
Upvotes: 64