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?

Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
giozh
  • 9,868
  • 30
  • 102
  • 183

3 Answers3

26

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

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
  • The reference tolds to override onDismiss. Now i have another trouble. How i can retrieve the mainview context where to create the toast? – giozh Mar 01 '13 at 17:53
  • 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. – Austyn Mahoney Mar 01 '13 at 18:06
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);
        }
    }

}
Community
  • 1
  • 1
Krzysiek
  • 7,895
  • 6
  • 37
  • 38
  • in onDismiss(..) how can I identify one dialog if I have multiple dialogs? – Anthea Apr 25 '16 at 15:51
  • When dialog is dismissed system passes reference to the dismissed dialog in `onDismiss(final DialogInterface dialog)` argument. See: http://developer.android.com/reference/android/app/DialogFragment.html#onDismiss(android.content.DialogInterface) – Krzysiek Apr 25 '16 at 20:37
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")
Tom Raganowicz
  • 2,169
  • 5
  • 27
  • 41
  • Hi, @NeverEndingQueue how I can do something like that to return a value o a accept button pressed? – Víctor Martín Dec 19 '22 at 16:25
  • 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. – Tom Raganowicz Dec 19 '22 at 17:13