You're being watched

This commit is contained in:
M66B 2022-04-22 10:56:53 +02:00
parent 12e577fc9a
commit d303a859a8
1 changed files with 37 additions and 23 deletions

View File

@ -144,6 +144,14 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
}
}
LifecycleObserver watcher = new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
cancel(context);
owner.getLifecycle().removeObserver(this);
}
};
future = getExecutor(context).submit(new Runnable() {
private Object data;
private long elapsed;
@ -181,7 +189,10 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
if (state.equals(Lifecycle.State.DESTROYED)) {
// No delivery
cleanup(context);
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
} else {
owner.getLifecycle().removeObserver(watcher);
if (state.isAtLeast(Lifecycle.State.RESUMED)) {
// Inline delivery
Log.i("Deliver task " + name + " state=" + state + " elapse=" + elapsed + " ms");
deliver();
@ -207,6 +218,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
});
}
}
}
private void deliver() {
try {
@ -249,6 +261,8 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
}
});
owner.getLifecycle().addObserver(watcher);
updateTaskCount(context);
}