Aviran
Aviran

Reputation: 5480

Destroy DialogFragment on onCreateDialog()

I have a dialog fragment that initializes Google plus views, sometimes those views fail so I'd like to kill the dialog at that point, before it's displayed to the user.

How can I end the dialog creation process? returning null from onCreateDialog which returns a Dialog object crushes the program.

Upvotes: 6

Views: 4410

Answers (3)

android developer
android developer

Reputation: 116040

The other answers are a bit outdated and one didn't work for me (wrote there my comments about it), so here's what I think is updated and working:

On the onCreateDialog callback, have your logic of when it succeeds. If failed, return some default dialog (won't be used anyway) while adding to the lifecycle's onStart callback to dismiss the DialogFragment (use dismiss or dismissAllowingStateLoss):

fun Lifecycle.runOnStarted(runnable: () -> Unit) {
    addObserver(object : DefaultLifecycleObserver {
        override fun onStart(owner: LifecycleOwner) {
            super.onStart(owner)
            runnable.invoke()
        }
    })
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    //create the builder of the dialog, and then check if need to dismiss
    if (needToDismiss) {
        lifecycle.runOnStarted{
            dismissAllowingStateLoss()
        }
        return builder.create()
    }

Alternative, using kotlin coroutines and without using the helper function I've made:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    //create the builder of the dialog, and then check if need to dismiss
    if (needToDismiss) {
        lifecycleScope.launch {
            whenStarted {
                dismissAllowingStateLoss()
            }
        }
        return builder.create()
    }

Or you can use this helper function that uses kotlin coroutines to make it a bit shorter:

fun LifecycleOwner.runOnStarted(runnable: () -> Unit) {
    lifecycleScope.launch {
        whenStarted{
            runnable.invoke()
        }
    }
}

The usage would be as short as before:

runOnStarted{
    dismissAllowingStateLoss()
}

Note that I use onStart callback instead of onCreate. The reason is that for some cases, onStart works, while onCreate won't.

Upvotes: 0

jazzgil
jazzgil

Reputation: 2366

If you'd like to dismiss DialogFragment within onCreateDialog you can do the following:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    setShowsDialog(false);
    dismiss();
    return null;
}

No need to override onActivityCreated().

Upvotes: 7

Aviran
Aviran

Reputation: 5480

Solved it using the onActivityCreated() Fragment callback which is called after OnCreateDialog(). I return a valid Dialog from onCreateDialog() but flag with dismiss that the dialog should be dismissed.

public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(dismiss) {
        this.dismiss();
    }
}

Upvotes: 3

Related Questions