I have static method in which i create a dialog
public static void showDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("ok", new OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}});
builder.setCancelable(false);
builder.create().show();
}
In my app there is can be a situation when method can be called several times, but i don't want to open 2 or more dialogs. How to check is the dialog opened or not ? Thanks...
You should put this code in every activity that you want to support this feature.
public AlertDialog myAlertDialog;
public void showDialog(Context context) {
if( myAlertDialog != null && myAlertDialog.isShowing() ) return;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}});
builder.setCancelable(false);
myAlertDialog = builder.create();
myAlertDialog.show();
}
Rewrite your method to return AlertDialog
, assign it to a member and check before invoking this method if it's null or !isShowing()
.
You could also use onCreateDialog
instead. Implement this method in base class for your activities that needs the dialog managing and then call showDialog(int id)
wherever you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With