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 天前
Glide 自定义圆角、铺满FitXY
glide
袁震3 天前
Android-Glide缓存机制
android·缓存·移动开发·glide
袁震8 天前
Android-Glide详解二
android·移动开发·glide
袁震8 天前
Android-Glide详解
android·移动开发·glide
飞翔的时光机9 天前
Glide 加载图片并应用滤镜效果
glide
zhangphil19 天前
Android矩阵Matrix实现Glide图像fitCenter转换为centerCrop,Kotlin
android·kotlin·glide
zhangphil1 个月前
Android Glide批量加载Bitmap,拼接组装大Bitmap,更新单个AppCompatImageView,Kotlin(2)
android·kotlin·glide
鸡鸭扣1 个月前
网络药理学之薛定谔Schrödinge Maestro:6、分子对接(Glide、Ligand docking)和可视化
glide·生物信息学·生信·分子对接·薛定谔·网络药理学·maestro
zhangphil1 个月前
Android Glide load origin Bitmap, Kotlin
android·kotlin·glide
dreamsever1 个月前
Glide源码学习
android·java·学习·glide