开发者

How to check is a dialog opened or not ?

开发者 https://www.devze.com 2023-02-24 08:06 出处:网络
I have static method in which i create a dialog public static void showDialog(Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context);

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.


            AlertDialog alertDialog;
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(msg);        
    alertDialog.setButton(-3, "Text Parsed", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

    alertDialog.show();
0

精彩评论

暂无评论...
验证码 换一张
取 消