How to dismiss AlertDialog.Builder?

What didn’t work about dismiss()? You should be able to use either Dialog.dismiss(), or Dialog.cancel() alertDialog.setNeutralButton(“Cancel”, new DialogInterface.OnClickListener() { // define the ‘Cancel’ button public void onClick(DialogInterface dialog, int which) { //Either of the following two lines should work. dialog.cancel(); //dialog.dismiss(); } });

How to dismiss AlertDialog in android

Actually there is no any cancel() or dismiss() method from AlertDialog.Builder Class. So Instead of AlertDialog.Builder optionDialog use AlertDialog instance. Like, AlertDialog optionDialog = new AlertDialog.Builder(this).create(); Now, Just call optionDialog.dismiss(); background.setOnClickListener(new OnClickListener() { public void onClick(View v) { SetBackground(); // here I want to dismiss it after SetBackground() method optionDialog.dismiss(); } });

Receive result from DialogFragment

Use myDialogFragment.setTargetFragment(this, MY_REQUEST_CODE) from the place where you show the dialog, and then when your dialog is finished, from it you can call getTargetFragment().onActivityResult(getTargetRequestCode(), …), and implement onActivityResult() in the containing fragment. It seems like an abuse of onActivityResult(), especially as it doesn’t involve activities at all. But I’ve seen it recommended by official google … Read more