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?