Glide系列-生命周期的监听

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

  • 在获得RequestManager的时候,调用new LifecycleLifecycle
  • RequestManager 与 Activity获得Fragment的Lifecycle对应
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 lifecycleListeners = new 
                                             HashSet();

    @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

你可能感兴趣的:(Glide,glide)