Glide系列-生命周期的监听

1.Glide.with

java 复制代码
public class Glide implements ComponentCallbacks2 {
    public static RequestManager with(@NonNull FragmentActivity activity) {
        return getRetriever(activity).get(activity);
    }
}

2.RequestManagerRetriever.get

java 复制代码
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

  • 在获得RequestManager的时候,调用new LifecycleLifecycle
  • RequestManager 与 Activity获得Fragment的Lifecycle对应
java 复制代码
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的对应的方法
java 复制代码
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

相关推荐
命运之手2 个月前
【Android】Glide模块工作原理
android·module·glide·工作原理·decoder
android1573 个月前
Android开发:Glide基本使用以及加载https图片(配合OkHttp实现)
android·okhttp·glide
bright7894 个月前
新版Glide检测生命周期原理
android·安卓·glide
一禅-小和尚4 个月前
LruCache、Glide和SmartRefreshLayout使用总结
glide
Sca_杰4 个月前
vue使用glide.js实现轮播图(可直接复制使用)
javascript·vue.js·glide
zhangphil5 个月前
Android Glide, first start based on loadThumbnail, Kotlin(二)
android·kotlin·glide
2401_856403035 个月前
AndroidX中Glide包冲突问题(1)
glide·androidx
大模型与计算机视觉5 个月前
扩散模型 GLIDE:35 亿参数的情况下优于 120 亿参数的 DALL-E 模型
人工智能·深度学习·大模型·glide·多模态·扩散模型·多模态大模型
zhangphil5 个月前
Android Glide, first start based on loadThumbnail, Kotlin(一)
android·kotlin·glide
starry陆离6 个月前
『项目整理』易CAR通项目说明文档-我的第一款APP
android·java·glide·项目经历