【问题标题】:How to show AlertDialog even if Activity is on background?即使 Activity 在后台,如何显示 AlertDialog?
【发布时间】:2021-08-20 13:07:42
【问题描述】:

我有一个 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();

【问题讨论】:

    标签: java android xml android-alertdialog


    【解决方案1】:

    您可以在 utils 类中创建一个方法并传递上下文,以便您可以在所需的类中使用对话框,但需要注意生命周期以避免内存泄漏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-16
      • 2020-03-11
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多