|
|
|
@ -8,20 +8,33 @@ import androidx.lifecycle.LifecycleRegistry;
|
|
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
|
|
|
|
|
|
|
|
|
public class TwoStateOwner implements LifecycleOwner {
|
|
|
|
|
private String name;
|
|
|
|
|
private LifecycleRegistry registry;
|
|
|
|
|
|
|
|
|
|
// https://developer.android.com/topic/libraries/architecture/lifecycle#lc
|
|
|
|
|
|
|
|
|
|
TwoStateOwner() {
|
|
|
|
|
TwoStateOwner(String aname) {
|
|
|
|
|
name = aname;
|
|
|
|
|
|
|
|
|
|
registry = new LifecycleRegistry(this);
|
|
|
|
|
registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
|
|
|
|
|
|
|
|
|
registry.addObserver(new LifecycleObserver() {
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
|
public void onAny() {
|
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
|
Log.i("LifeCycle " + name + " state=" + registry.getCurrentState());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TwoStateOwner(LifecycleOwner owner) {
|
|
|
|
|
this();
|
|
|
|
|
TwoStateOwner(LifecycleOwner owner, String aname) {
|
|
|
|
|
this(aname);
|
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
|
public void onDestroyed() {
|
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
|
Log.i("LifeCycle " + name + " parent destroyed");
|
|
|
|
|
destroy();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|