不关联生命周期的 observeForever 方法。

LiveData.java
------------------------------
@MainThread
public void observeForever(@NonNull Observer observer) {
assertMainThread("observeForever");
AlwaysActiveObserver wrapper = new AlwaysActiveObserver(observer);
ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
if (existing != null && existing instanceof LiveData.LifecycleBoundObserver) {
throw new IllegalArgumentException("Cannot add the same observer"
+ " with different lifecycles");
}
if (existing != null) {
return;
}
wrapper.activeStateChanged(true);
}

private class AlwaysActiveObserver extends ObserverWrapper {

AlwaysActiveObserver(Observer observer) {
super(observer);
}

@Override
boolean shouldBeActive() {
return true;
}
}
MutableLiveData 继承自 LiveData,把setValue(T) 和 getValue(T) 方法public公开了。


————————————————

转载于:https://www.cnblogs.com/hyhy904/p/11384643.html

你可能感兴趣的:(不关联生命周期的 observeForever 方法。)