I created the AlertDialog
using the builder. It shows when we call the show()
method. I have the cancel button in that dialog. I can cance
Move all the code of the builder outside of the onCreateDialog
method.
For instance here is the Android Dialogs guide updated :
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
mListener.onDialogNegativeClick(NoticeDialogFragment.this);
}
});
final Dialog dialog = builder.create();
DialogFragment fragment = new DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
return dialog;
}
};
fragment.show();
// and later ...
fragment.show();