FairEmail/app/src/main/java/eu/faircode/email/TwoStateOwner.java

45 lines
1.1 KiB
Java
Raw Normal View History

2019-03-06 16:36:20 +00:00
package eu.faircode.email;
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
2019-03-17 20:31:24 +00:00
import androidx.lifecycle.LifecycleObserver;
2019-03-06 16:36:20 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
2019-03-17 20:31:24 +00:00
import androidx.lifecycle.OnLifecycleEvent;
2019-03-06 16:36:20 +00:00
public class TwoStateOwner implements LifecycleOwner {
private LifecycleRegistry registry;
TwoStateOwner() {
registry = new LifecycleRegistry(this);
}
2019-03-17 20:31:24 +00:00
TwoStateOwner(LifecycleOwner owner) {
this();
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
}
});
}
2019-03-06 16:36:20 +00:00
void start() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_START);
}
void stop() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
}
2019-03-17 20:31:24 +00:00
void restart() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
start();
}
2019-03-06 16:36:20 +00:00
@NonNull
@Override
public Lifecycle getLifecycle() {
return registry;
}
}