From 5b5d7f04707afb80722cf2db5e65b036685b3a7e Mon Sep 17 00:00:00 2001
From: k3b <1374583+k3b@users.noreply.github.com>
Date: Fri, 1 Nov 2019 16:57:48 +0100
Subject: [PATCH] #153: Refactored AsyncTaskWithProgressDialog: progress dialog
can be re-attached after orientation change
---
.../widget/AsyncTaskWithProgressDialog.java | 31 ++++++++++++++-----
1 file changed, 23 insertions(+), 8 deletions(-)
diff --git a/app/src/main/java/de/k3b/android/widget/AsyncTaskWithProgressDialog.java b/app/src/main/java/de/k3b/android/widget/AsyncTaskWithProgressDialog.java
index 1af89d56..463eb369 100644
--- a/app/src/main/java/de/k3b/android/widget/AsyncTaskWithProgressDialog.java
+++ b/app/src/main/java/de/k3b/android/widget/AsyncTaskWithProgressDialog.java
@@ -36,13 +36,13 @@
abstract public class AsyncTaskWithProgressDialog extends AsyncTask
implements Closeable{
+ private final int idResourceTitle;
private WeakReference activity;
protected ProgressDialog dlg = null;
public AsyncTaskWithProgressDialog(Activity activity, int idResourceTitle) {
+ this.idResourceTitle = idResourceTitle;
this.setActivity(activity);
- dlg = new ProgressDialog(activity);
- dlg.setTitle(idResourceTitle);
}
protected void onProgressUpdate(String... values) {
if (dlg != null) {
@@ -73,10 +73,6 @@ public void close() {
destroy();
}
public void destroy() {
- if ((dlg != null) && dlg.isShowing()) {
- dlg.dismiss();
- }
- dlg = null;
setActivity(null);
}
@@ -101,7 +97,26 @@ protected Activity getActivity() {
return activity.get();
}
- private void setActivity(Activity activity) {
- this.activity = (activity != null) ? new WeakReference<>(activity) : null;
+ public void setActivity(Activity activity) {
+ boolean isActive = isNotFinishedYet();
+
+ if (isActive && (activity == getActivity())) {
+ // no change
+ return;
+ }
+ this.activity = (isActive && (activity != null)) ? new WeakReference<>(activity) : null;
+ if ((dlg != null) && dlg.isShowing()) {
+ dlg.dismiss();
+ }
+ dlg = null;
+
+ if ((activity != null) && isActive) {
+ dlg = new ProgressDialog(activity);
+ dlg.setTitle(idResourceTitle);
+ }
+ }
+
+ public boolean isNotFinishedYet() {
+ return (getStatus() != Status.FINISHED) && !isCancelled();
}
}