Daniel
Daniel

Reputation: 1033

AlertDialog doesn't close

I have an AlertDialog where positiveButton click should perform sharedPreferences string set changes, however, there is an issue with dialog not being closed after pressing positiveButton. What's more, after every click on the button it creates one more dialog so they accumulate...

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(android.R.string.dialog_alert_title);
            builder.setMessage("...");
            builder.setCancelable(false);
            builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Set<String> myNewSet = individualScheduleDays;//Default shared preference string set
                    myNewSet.remove(Integer.toString(scheduleID));
                    editor.remove(DAYS_WITH_INDIVIDUAL_SCHEDULE);
                    editor.commit();
                    editor.putStringSet(DAYS_WITH_INDIVIDUAL_SCHEDULE, myNewSet);
                    editor.commit();

                }
            });
            AlertDialog alert = builder.create();
            alert.show();

I found out that if I call editor.commit() only ones, everything is okay with dialog, but sharedPreference isn't changed.

Upvotes: 0

Views: 160

Answers (2)

nadir
nadir

Reputation: 83

Inside the onClick function of positiveButton you have to use

editor.dismiss();

Upvotes: 1

Dean Wild
Dean Wild

Reputation: 5964

Dialogs don't dismiss by themselves.

Try this:

//editor.remove(DAYS_WITH_INDIVIDUAL_SCHEDULE); // not needed
//editor.commit();     // not needed
editor.putStringSet(DAYS_WITH_INDIVIDUAL_SCHEDULE, myNewSet);
editor.commit();
dialog.dismiss();

Upvotes: 3

Related Questions