Fix possible IllegalArgumentException in GeoIP download

The dialog dismiss was called after the view was already destroyed
This commit is contained in:
emanuele-f 2023-02-04 11:45:57 +01:00
parent 3618f1e211
commit a1a7ea317f

View File

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