Java Code Examples for android.arch.lifecycle.LifecycleOwner#getLifecycle()

The following examples show how to use android.arch.lifecycle.LifecycleOwner#getLifecycle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LiveEventBus.java    From LiveEventBus with Apache License 2.0 6 votes vote down vote up
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
    SafeCastObserver<T> safeCastObserver = new SafeCastObserver<>(observer);
    //保存LifecycleOwner的当前状态
    Lifecycle lifecycle = owner.getLifecycle();
    Lifecycle.State currentState = lifecycle.getCurrentState();
    int observerSize = getLifecycleObserverMapSize(lifecycle);
    boolean needChangeState = currentState.isAtLeast(Lifecycle.State.STARTED);
    if (needChangeState) {
        //把LifecycleOwner的状态改为INITIALIZED
        setLifecycleState(lifecycle, Lifecycle.State.INITIALIZED);
        //set observerSize to -1,否则super.observe(owner, observer)的时候会无限循环
        setLifecycleObserverMapSize(lifecycle, -1);
    }
    super.observe(owner, safeCastObserver);
    if (needChangeState) {
        //重置LifecycleOwner的状态
        setLifecycleState(lifecycle, currentState);
        //重置observer size,因为又添加了一个observer,所以数量+1
        setLifecycleObserverMapSize(lifecycle, observerSize + 1);
        //把Observer置为active
        hookObserverActive(safeCastObserver, true);
    }
    //更改Observer的version
    hookObserverVersion(safeCastObserver);
}
 
Example 2
Source File: LifecycleScope.java    From rxjava-RxLife with Apache License 2.0 4 votes vote down vote up
static LifecycleScope from(LifecycleOwner owner, Event event) {
    return new LifecycleScope(owner.getLifecycle(), event);
}
 
Example 3
Source File: RxLifecycle.java    From RxLifecycle with Apache License 2.0 4 votes vote down vote up
public static RxLifecycle with(LifecycleOwner lifecycleOwner) {
    return new RxLifecycle(lifecycleOwner.getLifecycle());
}