DialogFragment Close Event
Asked Answered
W

3

12

I need to handle the end of a DialogFragment (after a call to .dismiss) - for example, I would show a toast inside the activity that "contains" the fragment after dismiss.

How do I handle the event?

Warrant answered 1/3, 2013 at 17:32 Comment(2)
You want to close the dialog, and then show it in a toast when it is dismissed?Sholes
yes, it's what i want to doWarrant
S
26

Override onDismiss() in your DialogFragment, or use setOnDismissListener() in the code block where you are building the fragment.

Sholes answered 1/3, 2013 at 17:39 Comment(2)
The reference tolds to override onDismiss. Now i have another trouble. How i can retrieve the mainview context where to create the toast?Warrant
Everyone would benefit if you created another question asking how to get the context. There are probably other answers you can find that have already answered this question also.Sholes
M
16

I faced similar problem, but I wanted to inform another activity about the dialog dismiss (not the activity that created and showed the dialog).

Although you can just override the onDismiss() method in your DialogFragment as Austyn Mahoney suggested, yet you can NOT use setOnDismissListener(), because DialogFragment simply does not provide such method (according to: Android Developers DialogFragment Reference).

But still there is another nice way to inform any other activity about the dialog dismiss, (I've found it there: DialogFragment and onDismiss), here it comes:

Firstly you should make your Activity (the one that you want to pass information about dialog dismiss) implement OnDismissListener:

public final class YourActivity extends Activity implements DialogInterface.OnDismissListener {

    @Override
    public void onDismiss(final DialogInterface dialog) {
        //Fragment dialog had been dismissed
    }

}

Again according to Android Developers DialogFragment Reference DialogFragment already implements OnDismissListener with onDismiss() method. That's why you should override it and call there your onDismiss() method which you implemented in YourActivity:

public final class DialogFragmentImage extends DialogFragment {

    @Override
    public void onDismiss(final DialogInterface dialog) {
        super.onDismiss(dialog);
        final Activity activity = getActivity();
        if (activity instanceof DialogInterface.OnDismissListener) {
            ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
        }
    }

}
Marcmarcano answered 12/7, 2015 at 10:17 Comment(2)
in onDismiss(..) how can I identify one dialog if I have multiple dialogs?Hysterical
When dialog is dismissed system passes reference to the dismissed dialog in onDismiss(final DialogInterface dialog) argument. See: developer.android.com/reference/android/app/…Marcmarcano
A
2

In your DialogFragment you can use this:

lateinit var onDismissListener : () -> Any

override fun onDismiss(dialog: DialogInterface) {
    if (this::onDismissListener.isInitialized) {
        onDismissListener()
    }

    super.onDismiss(dialog)
}

then in your fragment which creates the dialog:

val dialog = DialogFragment()
dialog.onDismissListener = {
      Toast.makeText(context, "Dismissed", Toast.LENGTH_SHORT).show()
}

dialog.show(context.supportFragmentManager, "tag")
Auctioneer answered 29/4, 2022 at 14:30 Comment(2)
Hi, @NeverEndingQueue how I can do something like that to return a value o a accept button pressed?Serviette
I am not sure, but you can probably pass the dialog to listener: onDismissListener(dialog) then in your onDismissListener = {} you can probably work out the value.Auctioneer

© 2022 - 2024 — McMap. All rights reserved.