implementation 'androidx.lifecycler:lifecycle-common:2.2.0' 替换上面引入的依赖(默认会集成上面的) implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0" 继承:DefaultLifecycleObserver,它是FullLifecycleObserver的子类,因为在Java8以后,支持interface接口类型,可以有自己的默认实现。 然后在Activity#OnCrea...
* Returns the current state of the Lifecycle. * * @return The current state of the Lifecycle. */ @MainThread @NonNull public abstract State getCurrentState(); Event?:应用组件生命周期事件的枚举类。 public enum Event { /** * Constant for onCreate event of the {@link LifecycleOwner}. */...
// alternately - if using Java8, use the following instead of lifecycle-compiler implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" // optional - helpers for implementing LifecycleOwner in a Service implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version" // ...
* Returns the current state of the Lifecycle. * * @return The current state of the Lifecycle. */ @MainThread @NonNull public abstract State getCurrentState(); @SuppressWarnings("WeakerAccess") public enum Event { /** * Constant for onCreate event of the {@link LifecycleOwner}. */ ...
public abstract class Lifecycle { @MainThread public abstract void addObserver(@NonNull LifecycleObserver observer); @MainThread public abstract void removeObserver(@NonNull LifecycleObserver observer); public enum Event { ON_CREATE, ON_START, ON_RESUME, ON_PAUSE, ON_STOP, ON_DESTROY, ON_ANY; }...
if(lifecycleinstanceofLifecycleRegistry) { ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event); } }ReportFragment.injectIfNeededIn(this)的作用是在Activity之上,创建一个不可见的Fragment的。当Fragment的生命周期发生变化,会通过dispatch(),接着调用((LifecycleRegistry) lifecycle).handleLifecycleEvent(event...
// ComponentActivity.java 类 @NonNull @Override public Lifecycle getLifecycle() { return mLifecycleRegistry; } 还可以看出,其实 ComponentActivity 只是实现了 LifecycleOwner 这个接口: // LifecycleOwner.java 接口文件 public interface LifecycleOwner { /** * Returns the Lifecycle of the provider. * *...
Lifecycle.java public abstract class Lifecycle { ... @MainThread public abstract void addObserver(@NonNull LifecycleObserver observer); } LifecycleRegistry.java @Override public void addObserver(@NonNull LifecycleObserver observer) { // 这里有一个 mState 的状态,我们后面讲 State initialState = mState...
if(lifecycleinstanceofLifecycleRegistry) { ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event); } } ReportFragment.injectIfNeededIn(this)的作用是在Activity之上,创建一个不可见的Fragment的。当Fragment的生命周期发生变化,会通过dispatch(),接着调用((LifecycleR...
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)fun create(){thread {Thread.sleep(4000)fun0()}}复制代码 两种情况对应的日志 不关闭页面等待耗时任务完成,耗时任务结束后查看日志: 在耗时任务结束前关闭页面,耗时任务结束后查看日志 结论 是使用LifeCycle的lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)方...