Improved two state lifecycles

This commit is contained in:
M66B
2019-04-13 08:02:01 +02:00
parent 722012609b
commit f93eb61b04
3 changed files with 31 additions and 16 deletions

View File

@@ -15,19 +15,7 @@ public class TwoStateOwner implements LifecycleOwner {
TwoStateOwner(String aname) {
name = aname;
// Initialize
registry = new LifecycleRegistry(this);
registry.markState(Lifecycle.State.CREATED);
// Logging
registry.addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
if (BuildConfig.DEBUG)
Log.i("LifeCycle " + name + " state=" + registry.getCurrentState());
}
});
create();
}
TwoStateOwner(LifecycleOwner owner, String aname) {
@@ -44,6 +32,20 @@ public class TwoStateOwner implements LifecycleOwner {
});
}
private void create() {
// Initialize
registry = new LifecycleRegistry(this);
registry.addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
if (BuildConfig.DEBUG)
Log.i("LifeCycle " + name + " state=" + registry.getCurrentState() + " " + registry);
}
});
registry.markState(Lifecycle.State.CREATED);
}
void start() {
registry.markState(Lifecycle.State.STARTED);
}
@@ -57,6 +59,11 @@ public class TwoStateOwner implements LifecycleOwner {
start();
}
void recreate() {
destroy();
create();
}
void destroy() {
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.CREATED))