Skip delivering results if task was destroyed

This commit is contained in:
M66B 2019-01-04 17:34:42 +00:00
parent 37360c4046
commit 857765727e
1 changed files with 22 additions and 16 deletions

View File

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