LiveData 为什么会初始化?
问题:
为什么有的时候在进入一些页面的时候,livedata.observe{ 这里的代码会执行 }?
LiveData源码解析
- setValue
@MainThread
protected void setValue(T value) {
assertMainThread("setValue");
mVersion++; //注意这个
mData = value;
dispatchingValue(null);
}
- dispatchingValue
//省略
considerNotify(iterator.next().getValue());
//省略
- considerNotify
private void considerNotify(ObserverWrapper observer) {
if (!observer.mActive) {
return;
}
// Check latest state b4 dispatch. Maybe it changed state but we didn't get the event yet.
//
// we still first check observer.active to keep it as the entrance for events. So even if
// the observer moved to an active state, if we'v