Skip delivering results if task was destroyed

pull/147/head
M66B 6 years ago
parent 37360c4046
commit 857765727e

@ -43,6 +43,7 @@ import androidx.lifecycle.OnLifecycleEvent;
public abstract class SimpleTask<T> implements LifecycleObserver { public abstract class SimpleTask<T> implements LifecycleObserver {
private LifecycleOwner owner; private LifecycleOwner owner;
private boolean paused; private boolean paused;
private boolean destroyed;
private Bundle args; private Bundle args;
private Result stored; private Result stored;
@ -105,7 +106,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
Log.i("Destroy task " + this); Log.i("Destroy task " + this);
owner.getLifecycle().removeObserver(this); owner.getLifecycle().removeObserver(this);
owner = null; owner = null;
paused = true; destroyed = true;
args = null; args = null;
stored = null; stored = null;
} }
@ -113,17 +114,18 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private void run(final Context context, LifecycleOwner owner, final Bundle args) { private void run(final Context context, LifecycleOwner owner, final Bundle args) {
this.owner = owner; this.owner = owner;
this.paused = false; this.paused = false;
this.destroyed = false;
this.args = null; this.args = null;
this.stored = null; this.stored = null;
owner.getLifecycle().addObserver(this);
try { try {
onPreExecute(args); onPreExecute(args);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }
owner.getLifecycle().addObserver(this);
final Handler handler = new Handler(); final Handler handler = new Handler();
// Run in background thread // Run in background thread
@ -151,27 +153,31 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
private void deliver(Bundle args, Result result) { private void deliver(Bundle args, Result result) {
if (destroyed)
return;
if (paused) { if (paused) {
Log.i("Deferring delivery task " + this); Log.i("Deferring delivery task " + this);
this.args = args; this.args = args;
this.stored = result; this.stored = result;
} else { return;
Log.i("Delivery task " + this); }
Log.i("Delivery task " + this);
try {
onPostExecute(args);
} catch (Throwable ex) {
Log.e(ex);
} finally {
try { try {
onPostExecute(args); if (result.ex == null)
onExecuted(args, (T) result.data);
else
onException(args, result.ex);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} finally {
try {
if (result.ex == null)
onExecuted(args, (T) result.data);
else
onException(args, result.ex);
} catch (Throwable ex) {
Log.e(ex);
}
onDestroyed();
} }
onDestroyed();
} }
} }

Loading…
Cancel
Save