Destroy DialogFragment on onCreateDialog()
Asked Answered
J

3

6

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.

Jude answered 24/7, 2013 at 6:47 Comment(0)
D
7

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

Dissolvent answered 19/3, 2016 at 9:21 Comment(2)
I would suggest returning a new Dialog() instead of null, since I had issues with NPEs on some systems. but all in all, yep, that is also a solution.Jude
You can't return null as the result has to be non null: developer.android.com/reference/androidx/fragment/app/… . Also, using this even with returning a non-null dialog, it still shows a transparent layer for me. This worked for me instead : https://mcmap.net/q/1681141/-destroy-dialogfragment-on-oncreatedialogSeko
J
3

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();
    }
}
Jude answered 24/7, 2013 at 17:41 Comment(2)
I managed to do this by calling dismiss() in onCreateDialog() itself.Noto
Works, but deprecated: use onViewCreated(View, Bundle) for code touching the Fragment's view and onCreate(Bundle) for other initialization. To get a callback specifically when a Fragment activity's Activity.onCreate(Bundle) is called, register a androidx.lifecycle.LifecycleObserver on the Activity's Lifecycle in onAttach(Context), removing it when it receives the Lifecycle.State.CREATED callback. . developer.android.com/reference/androidx/fragment/app/… . I've created a more updated answer: https://mcmap.net/q/1681141/-destroy-dialogfragment-on-oncreatedialogSeko
S
0

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.

Seko answered 11/9, 2021 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.