How to check if AlertDialog.builder is showing and cancelling it if its showing?
You can use AlertDialog
methods for that.
AlertDialog alert = new AlertDialog.Builder(context).create();if (alert.isShowing()) { alert.dismiss();}
Hope it helps.
An alternative approach is to use a method to generate the AlertDialog with a builder and then create the AlertDialog without showing it while setting the AlertDialog to a class variable.
Then check with .isShowing();
method
Example:
AlertDialog mAlertDialog;public showMyAlertDialog(View layout){ AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setView(layout); builder.setNeutralButton(getString(android.R.string.ok),new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); mAlertDialog = null; //setting to null is not required persay } }); mAlertDialog = builder.create() mAlertDialog.show();}public boolean isAlertDialogShowing(AlertDialog thisAlertDialog){ if(thisAlertDialog != null){ return thisAlertDialog.isShowing(); }}
hope that it is understood how to use this source.cheers
AlertDialog
extends Dialog
which has isShowing().
Hint: AlertDialog.Builder
creates an AlertDialog
instance. :)