1

I am refactoring a legacy android app. I created a util class called UxUtil and created a function which can be called from activity, to show dialog on success response.

    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;
    }

As you can see I am dismissing the dialog on positive button's onclick, and even in general.

From an activity, I call this function from volley response like this

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();
                            }
                        });

The extracting of AlertDialog into a function is working correctly in portion of code which is not upgraded to volley yet, i.e portion of legacy code which creates a new thread to perform network call and then runOnUiThread to show dialog.

But when I am upgrading it on volley to cleanup the threads, and running it from onResponse callback, Windows is leaking. Even though I am calling dismiss() on the button click. Why is that?

Abhinav Kulshreshtha
  • 2,147
  • 1
  • 23
  • 38
  • read https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added – IntelliJ Amiya Mar 16 '20 at 11:24
  • @IntelliJAmiya Already read that, I am using that as a stop gap solution by declaring a variable dialog at activity scope, and using it like (dialog = UxUtil.saveResponseDialog(context, jb.optString("ResponseMessage"))).show();, therefore having a dialog.dismiss at onDestroy. – Abhinav Kulshreshtha Mar 16 '20 at 11:59
  • @IntelliJAmiya I am trying to understand why it is leaking from volley but not in legacy code. And why it is not being dismissed even though I have called it on the positive button click. I have checked that setButton callback is being called when clicked. therefore dialog.dismiss() should have been called on button click. – Abhinav Kulshreshtha Mar 16 '20 at 12:01

1 Answers1

1

As I can show you code you also have to define the scope and context of the Dilaog becaus if the dialog is used in local or defined as local then you need to do it global and public so that other class can use it significantly.

  • I am setting context, Context is passed to targetContext parameter. The function is working with legacy code. But from within Volley onResponse callback, it is causing a leak. – Abhinav Kulshreshtha Mar 16 '20 at 12:03