如何从自定义对话框类关闭活动?

发布于 2024-11-28 00:56:23 字数 1191 浏览 1 评论 0原文

我有一个 AlertDialogUtils 类,它生成一个 AlertDialog ,以便在发生错误时可以从任何活动调用它。问题是我无法从 createDialog() 方法中调用 finish() 作为“关闭”按钮的 onClickListener

有什么想法这可能吗?

AlertDialogUtils 类的代码:

public class AlertDialogUtils extends Dialog {

    private Context mContext;

    public AlertDialogUtils(Context context) {
        super(context);
        mContext = context;
    }

    public void CreateAlertDialog(String errorMessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(errorMessage)
            .setCancelable(true)
            .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mContext.finish(); 
                    //error here. Intend to close the activtiy that created this dialog and has the error
                }
            });
        AlertDialog alert = builder.create();
        alert.setOwnerActivity((Activity)mContext); 
        // The dialog utils is outside an activity. Need to set owner
        alert.show();
    }
}

I have an AlertDialogUtils class that generates an AlertDialog such that it can be called from any activity when an error occurs. The issue is that I cannot call finish() from within the createDialog() method as a onClickListener for a "dismiss" button.

Any thoughts how this may be possible?

Code for AlertDialogUtils class:

public class AlertDialogUtils extends Dialog {

    private Context mContext;

    public AlertDialogUtils(Context context) {
        super(context);
        mContext = context;
    }

    public void CreateAlertDialog(String errorMessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(errorMessage)
            .setCancelable(true)
            .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mContext.finish(); 
                    //error here. Intend to close the activtiy that created this dialog and has the error
                }
            });
        AlertDialog alert = builder.create();
        alert.setOwnerActivity((Activity)mContext); 
        // The dialog utils is outside an activity. Need to set owner
        alert.show();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断舍离 2024-12-05 00:56:23

可以请你尝试一下这个方法吗? :

interface ICloseActivity {
void close();
}

class MyActivityToClose extends Activity implements ICloseActivity {

public void close() {
finish();
}

}

// -------

public class AlertDialogUtils extends Dialog {

    private Context mContext;

    private ICloseActivity mICloseActivity;

    public AlertDialogUtils(Context context, ICloseActivity aActivity) {
        super(context);
        mContext = context;
        mICloseActivity = aActivity;
    }

    public void CreateAlertDialog(String errorMessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(errorMessage)
           .setCancelable(true)
           .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
               //    mContext.finish(); 
              //error here. Intend to close the activtiy that created this dialog and has the error

                //TRY THIS please:
                mICloseActivity.close();
               }
           });
    AlertDialog alert = builder.create();
    alert.setOwnerActivity((Activity)mContext); 
          // The dialog utils is outside an activity. Need to set owner
    alert.show();
}

Could you please try it this way please? :

interface ICloseActivity {
void close();
}

class MyActivityToClose extends Activity implements ICloseActivity {

public void close() {
finish();
}

}

// -------

public class AlertDialogUtils extends Dialog {

    private Context mContext;

    private ICloseActivity mICloseActivity;

    public AlertDialogUtils(Context context, ICloseActivity aActivity) {
        super(context);
        mContext = context;
        mICloseActivity = aActivity;
    }

    public void CreateAlertDialog(String errorMessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(errorMessage)
           .setCancelable(true)
           .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
               //    mContext.finish(); 
              //error here. Intend to close the activtiy that created this dialog and has the error

                //TRY THIS please:
                mICloseActivity.close();
               }
           });
    AlertDialog alert = builder.create();
    alert.setOwnerActivity((Activity)mContext); 
          // The dialog utils is outside an activity. Need to set owner
    alert.show();
}
巷雨优美回忆 2024-12-05 00:56:23

不要在构造函数中传递 Context,而是传递 Activity。它继承了Context,因此您可以在任何需要Context的地方使用它;同时,您也可以在需要时调用finish()

Instead of passing a Context in the constructor, pass an Activity. It inherits Context, so you can use it anywhere you need a Context; at the same time, you can also call finish() when you need it.

温柔一刀 2024-12-05 00:56:23

感谢您的建议。不幸的是,ICloseActivity 对我来说不起作用。我通过以下方式解决了完成活动并关闭对话框的问题:
我按如下方式更改了 CreateAlertDialog() 方法,并创建了一个辅助函数 exitActivity()

public void CreateAlertDialog(String title, String errorMessage, int alertType){
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
             builder.setTitle(title)  
        .setIcon(R.drawable.alert_icon)
        .setMessage(errorMessage)
        .setCancelable(false)
        .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dismiss = true;
                           exitActivity();

                       }
                   });
        AlertDialog alert = builder.create();
        alert.setOwnerActivity((Activity)mContext);
            alert.show();
                }
             public void exitActivity() {
          this.getOwnerActivity().finish();
          }

在调用此实用程序时,我只需设置关闭侦听器并完成活动,以防万一被解雇了。

             dialog = new AlertDialogUtils(MyActivity.this);
    dialog.setOwnerActivity(MyActivity.this);
    dialog.CreateAlertDialog(getString(R.string.no_data),       
                        getString(R.string.empty_table_message), R.id.list_view_alertType);
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
        @Override
        public void onDismiss(DialogInterface arg0) {
            finish();
        }
    });

通过这种方式,如果需要,我可以处理关闭当前活动。
另外,对于我不想关闭而不是调用 exitActivity() 的活动,我调用了dialog.cancel():

               .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                cancel();

             }
        });

Thanks for the suggestions. ICloseActivity, unfortunately, did not work for me. I resolved the problem of finishing the activity and dismissing the dialog box in the following manner:
I changed the CreateAlertDialog() method as follows and created a helper function exitActivity():

public void CreateAlertDialog(String title, String errorMessage, int alertType){
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
             builder.setTitle(title)  
        .setIcon(R.drawable.alert_icon)
        .setMessage(errorMessage)
        .setCancelable(false)
        .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dismiss = true;
                           exitActivity();

                       }
                   });
        AlertDialog alert = builder.create();
        alert.setOwnerActivity((Activity)mContext);
            alert.show();
                }
             public void exitActivity() {
          this.getOwnerActivity().finish();
          }

While calling this utility, I simply set the dismiss listener and finish the activity incase it is dismissed.

             dialog = new AlertDialogUtils(MyActivity.this);
    dialog.setOwnerActivity(MyActivity.this);
    dialog.CreateAlertDialog(getString(R.string.no_data),       
                        getString(R.string.empty_table_message), R.id.list_view_alertType);
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
        @Override
        public void onDismiss(DialogInterface arg0) {
            finish();
        }
    });

In this way I can handle closing the current acitivity if required.
Also, for activities that I didn't want to close instead of calling exitActivity(), I called dialog.cancel():

               .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                cancel();

             }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文