接受生命周期事件并且做出相应处理

LifecycleBoundObserver.java
------------------------------
class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver {

@Override
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
removeObserver(mObserver);
return;
}
activeStateChanged(shouldBeActive());
}

@Override
boolean shouldBeActive() {
return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);
}


ObserverWrapper.java
------------------------------
private abstract class ObserverWrapper {

void activeStateChanged(boolean newActive) {
if (newActive == mActive) {
return;
}
// immediately set active state, so we'd never dispatch anything to inactive
// owner
mActive = newActive;
boolean wasInactive = LiveData.this.mActiveCount == 0;
LiveData.this.mActiveCount += mActive ? 1 : -1;
if (wasInactive && mActive) {
onActive();
}
if (LiveData.this.mActiveCount == 0 && !mActive) {
onInactive(http://www.my516.com);
}
if (mActive) {
dispatchingValue(this);
}
}
————————————————

posted @ 2019-08-19 20:26  李艳艳665  阅读(212)  评论(0编辑  收藏  举报