1.Glide.with
public class Glide implements ComponentCallbacks2 {
public static RequestManager with(@NonNull FragmentActivity activity) {
return getRetriever(activity).get(activity);
}
}
2.RequestManagerRetriever.get
public class RequestManagerRetriever implements Handler.Callback {
public RequestManager get(@NonNull FragmentActivity activity) {
if (Util.isOnBackgroundThread()) {//在后台线程
return get(activity.getApplicationContext());
}
assertNotDestroyed(activity);
frameWaiter.registerSelf(activity);
boolean isActivityVisible = isActivityVisible(activity);
Glide glide = Glide.get(activity.getApplicationContext());
return lifecycleRequestManagerRetriever.getOrCreate(
activity,
glide,
activity.getLifecycle(),
activity.getSupportFragmentManager(),
isActivityVisible);
}
}
3.LifecycleRequestManagerRetriever.getOrCreate
final class LifecycleRequestManagerRetriever {
RequestManager getOrCreate(
Context context,
Glide glide,
final Lifecycle lifecycle,
FragmentManager childFragmentManager,
boolean isParentVisible) {
//一个Lifecycle对应一个RequestManager
RequestManager result = getOnly(lifecycle);
if (result == null) {//result为null,才会去创建LifecycleLifecycle
LifecycleLifecycle glideLifecycle = new LifecycleLifecycle(lifecycle);
}
}
}
4.LifecycleLifecycle
- LifecycleLifecycle初始化的时候,给Activity或者Fragment的Lifecycle设置生命周期的监听
- 添加自定义的LifecycleListener
- 在各个生命周期的回调里调用自定义的LifecycleListener的对应的方法
final class LifecycleLifecycle implements Lifecycle, LifecycleObserver {
@NonNull
private final Set<LifecycleListener> lifecycleListeners = new
HashSet<LifecycleListener>();
@NonNull private final androidx.lifecycle.Lifecycle lifecycle;
LifecycleLifecycle(androidx.lifecycle.Lifecycle lifecycle) {
//这里是activity.getLifecycle()或者fragment.getLifecycle(),所以
//这里相当于设置了Activity或者Fragment的生命周期的监听。
this.lifecycle = lifecycle;
lifecycle.addObserver(this); //给Lifecycle注册LifecycleObserver
}
@OnLifecycleEvent(Event.ON_START)
public void onStart(@NonNull LifecycleOwner owner) {
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners))
{
lifecycleListener.onStart(); //Activity或者Fragment的onStart的时候执行
}
}
@OnLifecycleEvent(Event.ON_STOP)
public void onStop(@NonNull LifecycleOwner owner) {
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners))
{
lifecycleListener.onStop(); //Activity或者Fragment的onStop的时候执行
}
}
@OnLifecycleEvent(Event.ON_DESTROY)
public void onDestroy(@NonNull LifecycleOwner owner) {
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners))
{
lifecycleListener.onDestroy();
}
owner.getLifecycle().removeObserver(this); //移除掉生命周期的监听
}
@Override
public void addListener(@NonNull LifecycleListener listener) {
lifecycleListeners.add(listener); //添加自定义的LifecycleListener
if (lifecycle.getCurrentState() == State.DESTROYED) {
listener.onDestroy();
} else if (lifecycle.getCurrentState().isAtLeast(State.STARTED)) {
listener.onStart();
} else {
listener.onStop();
}
}
@Override
public void removeListener(@NonNull LifecycleListener listener) {
lifecycleListeners.remove(listener);//移除自定义的LifecycleListener
}
}
5.总结
所以在调用Glide.with的时候,就给Activity或者fragment添加了生命周期监听的observer
原文地址:https://blog.csdn.net/lostfish123/article/details/134795866
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_44866.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。