Dialogfragment.Getdialog Returns Null

DialogFragment.getDialog returns null

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.

Android DialogFragment getDialog() and getView() returning null

The problem I have here is that it is impossible to call setMyAction from the calling fragment and call the action(), since in the dialog, view is null just like if I try to get it with dialog().

The problem that you are experiencing now is that show() is asynchronous. The dialog will be created some time later, not by the time show() has returned.

The problem that you are not yet experiencing, but will, is that on a configuration change, your activity and its fragment will be recreated by default, and your OnClickListener will be lost.

Rather than pushing an event handler in, have the DialogFragment expose results (e.g., via a shared ViewModel).

A second problem here is: why is the onCreateView never called, just like onViewCreated?

You overrode onCreateDialog(). You cannot both use onCreateDialog() and onCreateView()/onViewCreated(). Pick one and use it.

getView's DialogFragment returning null on onDismiss

Old but gold. This one allows to have bigger control over whole fragment (for example when implementing seekBar or using ButterKnife). Enough said:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();

View view = inflater.inflate(R.layout.dialog_signin, null);
// do your stuff with views here

builder.setView(view)
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().dismiss();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}

I know I can save the view inflated in the OnCreateDialog as a attribute, but that doesn't seems right to me.

Yeah, that's the one. It looks right though, especially when implementing things like seekBar and while using libraries like ButterKnife.

DialogFragment getActivity() returns null after fragment processes other data

From what I can tell, during the time the Firebase transactions are happening, the AlertDialog and DialogFragment are dismissed automatically and I can no longer get the context to set up my intent.

AFAIK This is the exact reason.

Solution 1

Pass the validated data to Hosting Activity and do firebase writes there. This way you will always have context to start another activity.

Solution 2

Don't dismiss Dialog (Show ProgressBar ) until your firebase writes are done and ConversationListActivity is started.

android: how do I check if dialogfragment is showing

simply check if it's null

if(prev == null)
//There is no active fragment with tag "dialog"
else
//There is an active fragment with tag "dialog" and "prev" variable holds a reference to it.

Alternatively, you could check the activity the fragment prev is currently associated with, however, make sure you ask that after you make sure it's not null or you'll get a NullPointerException. Like this:

if(prev == null)
//There is no active fragment with tag "dialog"
else
if(prev.getActivity() != this) //additional check
//There is a fragment with tag "dialog", but it is not active (shown) which means it was found on device's back stack.
else
//There is an active fragment with tag "dialog"

Android DialogFragment getActivity() is null

DialogFragment.show() is asynchronous -- the DialogFragment isn't actually immediately displayed -- it's sent to the end of the message queue. I'd suggest providing the timeout value as an argument, and then set it in onCreateDialog() along with the title. For example:

public class SessionInactivityDialog extends DialogFragment {
public static final String EXTRA_TIMEOUT = "timeout";

public static SessionInactivityDialog newInstance(long timeout) {
Bundle args = new Bundle();
args.putLong(EXTRA_TIMEOUT, timeout);
SessionInactivityDialog dialog = new SessionInactivityDialog();
dialog.setArguments(args);
return dialog;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final SessionActivity activity = (SessionActivity) getActivity();
final long timeout = getArguments().getLong(EXTRA_TIMEOUT);
final String msg = activity.getString(R.string.imminent_logout_text, (timeout / 1000));

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.imminent_logout);
builder.setMessage(msg);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
activity.resetTimer();
}
});
return builder.create();
}
}

Then you can just show it with:

SessionInactivityDialog.newInstance(timeout)
.show(getFragmentManager(), TAG);

I'd also suggest making an interface for the dialog to communicate with the activity, rather than adding the direct dependency on SessionActivity (that way you can reuse this in any Activity, as long as it implements your interface).



Related Topics



Leave a reply



Submit