Skip to content

Commit

Permalink
#153: Refactored AsyncTaskWithProgressDialog: progress dialog can be …
Browse files Browse the repository at this point in the history
…re-attached after orientation change
  • Loading branch information
k3b committed Nov 1, 2019
1 parent 2bb6caf commit 5b5d7f0
Showing 1 changed file with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@

abstract public class AsyncTaskWithProgressDialog<param> extends AsyncTask<param, String, Integer>
implements Closeable{
private final int idResourceTitle;
private WeakReference<Activity> 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) {
Expand Down Expand Up @@ -73,10 +73,6 @@ public void close() {
destroy();
}
public void destroy() {
if ((dlg != null) && dlg.isShowing()) {
dlg.dismiss();
}
dlg = null;
setActivity(null);
}

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

0 comments on commit 5b5d7f0

Please sign in to comment.