Reputation: 4201
Must dismiss() be called in onClick? If I didn't call dismiss(), the dialog still can dismiss.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Bundle args = getArguments();
if (args.containsKey(ARG_MESSAGE_INT)) {
builder.setMessage(args.getInt(ARG_MESSAGE_INT));
} else if (args.containsKey(ARG_MESSAGE_STRING)) {
builder.setMessage(args.getString(ARG_MESSAGE_STRING));
}
if (args.containsKey(ARG_TITLE_INT)) {
builder.setTitle(args.getInt(ARG_TITLE_INT));
} else if (args.containsKey(ARG_TITLE_STRING)) {
builder.setTitle(args.getString(ARG_TITLE_STRING));
}
builder
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (null != buttonOnClickListener)
buttonOnClickListener.onPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (null != buttonOnClickListener)
buttonOnClickListener.onNegativeClick();
}
}
);
return builder.create();
}
Upvotes: 1
Views: 513
Reputation: 29320
If you use the builder to create the dialog and set your listeners there, then clicking on any button will automatically dismiss the dialog, you do not need to call dismiss.
If you override the click listeners to prevent the dialog from dismissing automatically (maybe because you want to validate some data before letting the dialog go), you would then need to call dismiss. To do this, you would override the click listeners in onStart.
Upvotes: 3