Added lifecycle logging

This commit is contained in:
M66B
2019-03-27 13:24:58 +00:00
parent e3f018aaa2
commit e4e64e21c6
5 changed files with 21 additions and 8 deletions

View File

@@ -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();
}
});