Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check is a dialog opened or not ?

Tags:

android

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...

like image 218
Jim Avatar asked Apr 14 '11 08:04

Jim


2 Answers

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();
}
like image 75
vendor Avatar answered Sep 24 '22 08:09

vendor


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.

like image 30
ernazm Avatar answered Sep 24 '22 08:09

ernazm