Prevent crash

This commit is contained in:
M66B 2020-03-12 13:40:07 +01:00
parent 2c123b7902
commit c7ba0cb058
1 changed files with 25 additions and 5 deletions

View File

@ -62,17 +62,17 @@ public class TwoStateOwner implements LifecycleOwner {
}
});
registry.setCurrentState(Lifecycle.State.CREATED);
setState(Lifecycle.State.CREATED);
}
void start() {
if (!registry.getCurrentState().equals(Lifecycle.State.DESTROYED))
registry.setCurrentState(Lifecycle.State.STARTED);
setState(Lifecycle.State.STARTED);
}
void stop() {
if (!registry.getCurrentState().equals(Lifecycle.State.DESTROYED))
registry.setCurrentState(Lifecycle.State.CREATED);
setState(Lifecycle.State.CREATED);
}
void restart() {
@ -88,9 +88,9 @@ public class TwoStateOwner implements LifecycleOwner {
void destroy() {
Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.CREATED))
registry.setCurrentState(Lifecycle.State.CREATED);
setState(Lifecycle.State.CREATED);
if (!state.equals(Lifecycle.State.DESTROYED))
registry.setCurrentState(Lifecycle.State.DESTROYED);
setState(Lifecycle.State.DESTROYED);
}
@NonNull
@ -98,4 +98,24 @@ public class TwoStateOwner implements LifecycleOwner {
public Lifecycle getLifecycle() {
return registry;
}
private void setState(@NonNull Lifecycle.State state) {
try {
registry.setCurrentState(state);
} catch (Throwable ex) {
Log.e(ex);
/*
java.lang.RuntimeException: Failed to call observer method
at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(SourceFile:226)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(SourceFile:194)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(SourceFile:186)
at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(SourceFile:37)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(SourceFile:361)
at androidx.lifecycle.LifecycleRegistry.backwardPass(SourceFile:316)
at androidx.lifecycle.LifecycleRegistry.sync(SourceFile:334)
at androidx.lifecycle.LifecycleRegistry.moveToState(SourceFile:145)
at androidx.lifecycle.LifecycleRegistry.setCurrentState(SourceFile:118)
*/
}
}
}