简体   繁体   English

即使 Activity 在后台,如何显示 AlertDialog?

[英]How to show AlertDialog even if Activity is on background?

I have an AlertDialog and I want to show it even if the user is visiting another activity.我有一个 AlertDialog,即使用户正在访问另一个活动,我也想显示它。

This is AlertDialog:这是警报对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());

    builder.setMessage("Message").setCancelable(
            false).setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

You can create a method in utils class and pass context, so that you can use dialog in required class, but precaution need to take with life cycles to avoid memory leaks.您可以在 utils class 中创建一个方法并传递上下文,以便您可以在所需的 class 中使用对话框,但需要采取预防措施来避免 memory 泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM