From a1a7ea317fa8f45b9264346740ecad50f0d0402f Mon Sep 17 00:00:00 2001 From: emanuele-f Date: Sat, 4 Feb 2023 11:45:57 +0100 Subject: [PATCH] Fix possible IllegalArgumentException in GeoIP download The dialog dismiss was called after the view was already destroyed --- .../fragments/GeoipSettings.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/emanuelef/remote_capture/fragments/GeoipSettings.java b/app/src/main/java/com/emanuelef/remote_capture/fragments/GeoipSettings.java index 4b2d869a..1442dd38 100644 --- a/app/src/main/java/com/emanuelef/remote_capture/fragments/GeoipSettings.java +++ b/app/src/main/java/com/emanuelef/remote_capture/fragments/GeoipSettings.java @@ -42,6 +42,7 @@ public class GeoipSettings extends PreferenceFragmentCompat { private static final String TAG = "GeoipSettings"; private Preference mStatus; private Preference mDelete; + private AlertDialog mAlertDialog; @Override public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) { @@ -65,6 +66,15 @@ public class GeoipSettings extends PreferenceFragmentCompat { }); } + @Override + public void onDestroyView() { + // See https://stackoverflow.com/questions/22924825/view-not-attached-to-window-manager-crash + if(mAlertDialog != null) + mAlertDialog.dismiss(); + + super.onDestroyView(); + } + // NOTE: passing explicit context as this may be called when requireContext would return null private void refreshStatus(Context context) { Date builtDate = Geolocation.getDbDate(context); @@ -90,14 +100,15 @@ public class GeoipSettings extends PreferenceFragmentCompat { builder.setTitle(R.string.downloading); builder.setMessage(R.string.download_in_progress); - final AlertDialog alert = builder.create(); - alert.setCanceledOnTouchOutside(false); - alert.show(); + mAlertDialog = builder.create(); + mAlertDialog.setCanceledOnTouchOutside(false); + mAlertDialog.show(); - alert.setOnCancelListener(dialogInterface -> { + mAlertDialog.setOnCancelListener(dialogInterface -> { Log.i(TAG, "Abort download"); executor.shutdownNow(); }); + mAlertDialog.setOnDismissListener(dialog -> mAlertDialog = null); // Hold reference to context to avoid garbage collection before the handler is called final Context context = requireContext(); @@ -108,7 +119,8 @@ public class GeoipSettings extends PreferenceFragmentCompat { if(!result) Utils.showToastLong(context, R.string.download_failed); - alert.dismiss(); + if(mAlertDialog != null) + mAlertDialog.dismiss(); refreshStatus(context); }); });