0

I am not using the negative and positive buttons. I need to close the dialog but dialog.dismiss() has no effect.

        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setView(view);
        alert.setCancelable(false);
        dialog = alert.create();
        goButton.setOnClickListener(new View.OnClickListener() { //goButton is inside view which is inflated inside the dialog

            @Override
            public void onClick(View view) {
                age = (String)spinner.getSelectedItem();
                if(gender == null){
                    Utils.makeToast(context, "Select your gender");
                }else if(age == null || age.toLowerCase().contains("age") || age.equals("")){
                    spinner.performClick();
                }else{
                    Utils.makeToast(context, (String)spinner.getSelectedItem() + " - gender: " +gender);
                    editor.putInt("age", Utils.getIntOrZero(age));
                    editor.putString("gender", gender);
                    editor.commit();
                    dialog.dismiss(); // called but not working
                }
            }
        });

        alert.show();
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
124697
  • 22,097
  • 68
  • 188
  • 315

1 Answers1

13

It isn't dismissing because the AlertDialog you're calling AlertDialog.dismiss on isn't the same one that's shown. In other words, you're calling alert.show() and using dialog.dismiss(). To fix it call dialog.show().

adneal
  • 30,484
  • 10
  • 122
  • 151