19

My activity opens a dialog. When it closes I need the function ReloadTable() to be executed. So I am trying to use setOnDismissListener but its not getting triggered. Could someone please help what I am doing wrong?

Thanks!

AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.transaction, null);
builder = new AlertDialog.Builder(new ContextThemeWrapper(TransactionsList.this , R.style.dialogwithoutdim));
builder.setView(layout);
alertDialog = builder.create();
alertDialog.setOnDismissListener(new OnDismissListener() {
    public void onDismiss(final DialogInterface dialog) {
        ReloadTable();
    }
});

builder.show();
Xavi
  • 20,111
  • 14
  • 72
  • 63
lumpawire
  • 468
  • 1
  • 7
  • 16
  • This is the reason it doesn't work for you, probably: http://stackoverflow.com/a/18269965/89818 – caw Jan 19 '15 at 04:01

6 Answers6

13
public class MyActivity extends Activity implements DialogInterface.OnCancelListener{
    @Override
    public void onCreate(Bundle state) {
       .....
       alertDialog.setOnCancelListener(this);
       alertDialog.show();
    }
    @Override
    public void onCancel(DialogInterface dialog) {
        dialog.dismiss();
        .....
    }
}
sth
  • 222,467
  • 53
  • 283
  • 367
Zeev G
  • 2,191
  • 21
  • 37
9

You have to setOnCancelListener to the AlertDialog.Builder:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialogmenu = false;
            }
        })
Java Geek
  • 337
  • 4
  • 6
4

OK...I figured it out myself.

I had to implement DialogInterface.OnCancelListener and add the onCancel() method. It worked!

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
lumpawire
  • 468
  • 1
  • 7
  • 16
  • 5
    `setOnDismissListener` is only available from API 17, I guess that was your problem. – Yoann Hercouet Apr 26 '14 at 14:54
  • @YoannHercouet Actually, API level 1?! http://developer.android.com/reference/android/app/Dialog.html#setOnDismissListener%28android.content.DialogInterface.OnDismissListener%29 – caw Jan 19 '15 at 03:55
  • 1
    Only for set through AlerDialog.Builder. In AlertDialog it since API level 1. – Enyby Aug 03 '15 at 03:30
4

Use following code

final AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
                final View dailogView = LayoutInflater.from(MyActivity.this).inflate(R.layout.dialog_layout, null);
                builder.setView(dailogView);
                final AlertDialog dialog=builder.create();
                dialog.show();

DismissListener

 dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                   // your code after dissmiss dialog     
                    }
                });
Vishal Nagvadiya
  • 1,178
  • 12
  • 15
3

In this case you should use alertDialog.setOnCancelListener(listener),and alertDialog.setOnDismissListener works with dismissDialog(id).

Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • This does not work. I am not dismissing the dialog. I just expect the back button be pressed to dismiss the dialog... why would the dismiss not trigger? – lumpawire Sep 21 '12 at 08:00
  • @Override public void onBackPressed() { dismissDialog(id) super.onBackPressed(); } Have you tried setOnCancelListener with back button pressed? – Yahor10 Sep 21 '12 at 08:12
  • Create alertDialog alerDialog field and cancel by object in OnBackPressed() function : dialog.cancel(); – Yahor10 Sep 21 '12 at 08:30
2

I found the real problem.

You should call .show in the dialog, not in the builder.

Try it :)

RogerParis
  • 1,549
  • 1
  • 13
  • 24