Java Code Examples for android.arch.lifecycle.Lifecycle#State

The following examples show how to use android.arch.lifecycle.Lifecycle#State . 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: LiveEventBus.java    From LiveEventBus with Apache License 2.0 5 votes vote down vote up
private void setLifecycleState(Lifecycle lifecycle, Lifecycle.State state) {
    if (lifecycle == null) {
        return;
    }
    if (!(lifecycle instanceof LifecycleRegistry)) {
        return;
    }
    try {
        Field mState = LifecycleRegistry.class.getDeclaredField("mState");
        mState.setAccessible(true);
        mState.set(lifecycle, state);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: Utility.java    From scene with Apache License 2.0 4 votes vote down vote up
private static String getViewMessage(Scene scene, int marginOffset) {
    String tag = null;
    boolean isHidden = false;
    String status = null;
    if (scene.getParentScene() instanceof GroupScene) {
        GroupScene groupScene = (GroupScene) scene.getParentScene();
        tag = groupScene.findTagByScene(scene);
        isHidden = !groupScene.isShow(scene);
    } else if (scene.getParentScene() instanceof NavigationScene) {
        Lifecycle.State state = scene.getLifecycle().getCurrentState();
        if (state == Lifecycle.State.RESUMED) {
            status = "resumed";
        } else if (state == Lifecycle.State.STARTED) {
            status = "paused";
        } else if (state == Lifecycle.State.CREATED) {
            status = "stopped";
        }
    }

    String repeated = new String(new char[marginOffset]).replace("\0", "    ");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(repeated + "[" + scene.getClass().getSimpleName() + "] ");

    if (tag != null) {
        stringBuilder.append("tag: " + tag + " ");
        if (isHidden) {
            stringBuilder.append("hidden ");
        }
    }

    if (status != null) {
        stringBuilder.append("status: " + status + " ");
    }

    String resourceId = null;
    if (scene.getApplicationContext() != null && scene.getView() != null && scene.getView().getId() != View.NO_ID) {
        resourceId = getIdName(scene.requireApplicationContext(), scene.getView().getId());
    }
    if (resourceId != null) {
        stringBuilder.append("viewId: " + resourceId + " ");
    }
    stringBuilder.append("\n");
    return stringBuilder.toString();
}
 
Example 4
Source File: LiveEventBus.java    From LiveEventBus with Apache License 2.0 4 votes vote down vote up
@Override
protected Lifecycle.State observerActiveLevel() {
    return lifecycleObserverAlwaysActive ? Lifecycle.State.CREATED : Lifecycle.State.STARTED;
}
 
Example 5
Source File: LiveEventBusCore.java    From LiveEventBus with Apache License 2.0 4 votes vote down vote up
@Override
protected Lifecycle.State observerActiveLevel() {
    return lifecycleObserverAlwaysActive ? Lifecycle.State.CREATED : Lifecycle.State.STARTED;
}
 
Example 6
Source File: LiveEvent.java    From LiveEventBus with Apache License 2.0 2 votes vote down vote up
/**
 * determine when the observer is active, means the observer can receive message
 * the default value is CREATED, means if the observer's state is above create,
 * for example, the onCreate() of activity is called
 * you can change this value to CREATED/STARTED/RESUMED
 * determine on witch state, you can receive message
 *
 * @return
 */
protected Lifecycle.State observerActiveLevel() {
    return CREATED;
}