【问题标题】:Android Window is leaking even though .dismiss() is called on dialog inside Volley onResponse即使在 Volley onResponse 内的对话框中调用了 .dismiss(),Android 窗口仍在泄漏
【发布时间】:2020-03-16 11:14:30
【问题描述】:

我正在重构一个旧版 Android 应用程序。我创建了一个名为 UxUtil 的实用程序类,并创建了一个可以从活动中调用的函数,以显示有关成功响应的对话框。

    private static AlertDialog simpleAlertDialog(final Context targetContext, String title, String message) {
        AlertDialog alertDialog = new AlertDialog.Builder(targetContext).create();
        alertDialog.setCanceledOnTouchOutside(false)
        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.cslogo_icon_small);
        // Setting Dialog Title
        alertDialog.setTitle(title);
        // Setting Dialog Message
        alertDialog.setMessage(message);
        alertDialog.dismiss();

        // Setting OK Button
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (dialog, which) -> {
            dialog.dismiss();
            // Write your code here to execute after dialog closed
            ((Activity) targetContext).finish();
            ((Activity) targetContext).overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        });
        return alertDialog;
    }

如您所见,我正在关闭正面按钮的 onclick 对话框,甚至一般情况下也是如此。

从一个活动中,我从这样的凌空响应中调用这个函数

btn_save.setOnClickListener(v -> performSave());

private void performSave(){
   // get data from UI for sending to server in a bean object called parcelObject.
 ServiceCall.saveGeneralDetail(parcelObject, username,
                        new VolleyResponseListener() {
                            @Override
                            public void onResponse(Object response) throws JSONException {
                                if (response != null) {
                                    JSONObject jb = new JSONObject(response.toString());
                                    UxUtil.simpleAlertDialog(context, jb.optString("ResponseMessage")).show();
                                }
                            }
                        }

                            @Override
                            public void onError(String message) {
                                UxUtil.simpleAlertDialog(context, message).show();
                            }
                        });

在尚未升级到 volley 的部分代码中,将 AlertDialog 提取到函数中可以正常工作,即创建新线程以执行网络调用然后运行 ​​OnUiThread 以显示对话框的旧代码部分。

但是当我在 volley 上升级它以清理线程并从 onResponse 回调运行它时,Windows 正在泄漏。即使我在单击按钮时调用了dismiss()。这是为什么呢?

【问题讨论】:

  • @IntelliJAmiya 已经读过,我将其用作权宜之计,方法是在活动范围内声明一个变量对话框,并像 (dialog = UxUtil.saveResponseDialog(context, jb.optString(" ResponseMessage"))).show();,因此在 onDestroy 处有一个 dialog.dismiss。
  • @IntelliJAmiya 我试图理解为什么它会从 volley 中泄漏,但不会在遗留代码中泄漏。以及为什么即使我在点击肯定按钮时调用它也没有被解雇。我检查了单击时是否调用了 setButton 回调。因此 dialog.dismiss() 应该在按钮点击时被调用。

标签: android android-volley android-alertdialog


【解决方案1】:

正如我可以向您展示的代码,您还必须定义 Dilaog 的范围和上下文,因为如果对话框用于本地或定义为本地,那么您需要将其设为全局和公共,以便其他类可以显着使用它.

【讨论】:

  • 我正在设置上下文,上下文被传递给 targetContext 参数。该函数正在使用遗留代码。但是在 Volley onResponse 回调中,它导致了泄漏。
猜你喜欢
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多