1、LoaderManager.getInstance(this).initLoader(BASE_LODER_ID,null,this);
a)getinstance()方法返回的实例对象的类型是loadermanagerimplement。
b)loadermanagerimplement.initLoader()方法:
其中核心方法:直接调用callback的onCreateLoader()方法生成loader对象。然后把loader对象封装成LoaderInfo对象。然后调用LoderInfo.setCallback()方法。
setcallback方法内部生成LoaderObserver对象,这个对象维护这callback回调对象。初始化到此结束。
数据结构:
2、public void onLoadFinished(@NonNull Loader<P> loader, P data)
这个回调什么时候回调呢?其实答案在上面的数据结构图已经给出了。无非就是通过LiveData进行activity、fragment的生命周期变化的监听,当生命周期处于start或者resume状态认为是一种active状态,会触发loader对象的startloading方法
和onstartloading回调方法。当生命周期处于destroy的时候当然也会自动销毁对应的loader对象。
那问题来了,onLoadFinished这个回调是怎么触发的呢。其实也是livedata的机制啦。
通过初始化的时候生成的loaderobserver对象进行监听,然后loaderobserver内部调用回调对象而已。
3、LoaderInfo源码
4、LiveData
LiveData is a data holder class that can be observed within a given lifecycle.
* This means that an {@link Observer} can be added in a pair with a {@link LifecycleOwner}, and
* this observer will be notified about modifications of the wrapped data only if the paired
* LifecycleOwner is in active state. LifecycleOwner is considered as active, if its state is
* {@link Lifecycle.State#STARTED} or {@link Lifecycle.State#RESUMED}. An observer added via
* {@link #observeForever(Observer)} is considered as always active and thus will be always notified
* about modifications. For those observers, you should manually call
* {@link #removeObserver(Observer)}.
这段注释很重要。1)livedata数据容器;2)有lifeowner和observer机制,生命周期监听就交给lifecycle机制了。
1)数据更新方法:
5、loadermanager如果保证屏幕旋转的情况下保证loader对象唯一的