面试之Glide如何绑定Activity的生命周期

Glide绑定Activity生命周期

Glide.with()

下面都是它的重载方法,Context,Activity,FragmentActivity, Fragment, android.app.Fragment fragment,View都可以作为他的参数,内容大同小异,都是先getRetriever,然后再get

@NonNull
  public static RequestManager with(@NonNull Context context) {
    return getRetriever(context).get(context);
  }

  @NonNull
  public static RequestManager with(@NonNull Activity activity) {
    return getRetriever(activity).get(activity);
  }
  
  @NonNull
  public static RequestManager with(@NonNull FragmentActivity activity) {
    return getRetriever(activity).get(activity);
  }
  
  @NonNull
  public static RequestManager with(@NonNull Fragment fragment) {
    return getRetriever(fragment.getContext()).get(fragment);
  }

  @SuppressWarnings("deprecation")
  @Deprecated
  @NonNull
  public static RequestManager with(@NonNull android.app.Fragment fragment) {
    return getRetriever(fragment.getActivity()).get(fragment);
  }

  @NonNull
  public static RequestManager with(@NonNull View view) {
    return getRetriever(view.getContext()).get(view);
  }
getRetriever()

原来获取的是Glide里面RequestManagerRetriever,Glide是个双重校验实现的单例

  @NonNull
  @NonNull
  private static RequestManagerRetriever getRetriever(@Nullable Context context) {
     ...
    return Glide.get(context).getRequestManagerRetriever();
  }
  
    public static Glide get(@NonNull Context context) {
    if (glide == null) {
      GeneratedAppGlideModule annotationGeneratedModule =
          getAnnotationGeneratedGlideModules(context.getApplicationContext());
      synchronized (Glide.class) {
        if (glide == null) {
          checkAndInitializeGlide(context, annotationGeneratedModule);
        }
      }
    }

    return glide;
  }

Glide的 getRequestManagerRetriever返回了requestManagerRetriever属性

面试之Glide如何绑定Activity的生命周期_第1张图片

面试之Glide如何绑定Activity的生命周期_第2张图片
返回到第一步with方法

    return getRetriever(context).get(activity);

查看RequestManagerRetriver的get() 主要关注框出来的部分
获取到FragmentManeger 调用FragmentGet()
面试之Glide如何绑定Activity的生命周期_第3张图片
实例化了一个RequestManagerFragment, 然后给这个Fragment指定了一个RequestManager,最终返回这个fragment里面的requestManager;

  @Deprecated
  @NonNull
  private RequestManager fragmentGet(
      @NonNull Context context,
      @NonNull android.app.FragmentManager fm,
      @Nullable android.app.Fragment parentHint,
      boolean isParentVisible) {
      
    RequestManagerFragment current = getRequestManagerFragment(fm, parentHint);
    RequestManager requestManager = current.getRequestManager();
    if (requestManager == null) {
      // TODO(b/27524013): Factor out this Glide.get() call.
      Glide glide = Glide.get(context);
      
      //实例化requsetManager
      requestManager =
          factory.build(
              glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);

      if (isParentVisible) {
        requestManager.onStart();
      }
      current.setRequestManager(requestManager);
    }
    return requestManager;
  }

 @NonNull
  private RequestManagerFragment getRequestManagerFragment(
      @NonNull final android.app.FragmentManager fm, @Nullable android.app.Fragment parentHint) {
    RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
    if (current == null) {
      current = pendingRequestManagerFragments.get(fm);
      if (current == null) {
        ///创建Fragment
        current = new RequestManagerFragment();
        
        current.setParentFragmentHint(parentHint);
        pendingRequestManagerFragments.put(fm, current);

        // 将这个Frament添加到了Activity中
        fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
        
        handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
      }
    }
    return current;
  }

有没有注意到实例化RequestManager时传入的参数,其中有一个是Fragment的LifeCycle

     requestManager =
          factory.build(
              glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);

看到RequestManagerFragment里面确实例化了一个LifeCycle对象

  public RequestManagerFragment() {
    this(new ActivityFragmentLifecycle());
  }

  @VisibleForTesting
  @SuppressLint("ValidFragment")
  RequestManagerFragment(@NonNull ActivityFragmentLifecycle lifecycle) {
    this.lifecycle = lifecycle;
  }

在RequestManager中给这个LifeCycle注册了一个观察者
面试之Glide如何绑定Activity的生命周期_第4张图片

面试之Glide如何绑定Activity的生命周期_第5张图片
这样在Fragment 生命周期发生变化时,RequestManager也能感受到了
面试之Glide如何绑定Activity的生命周期_第6张图片

小结

Gilde能感受Activity生命周期,在onStop的时候停止加载,在onStart的时候继续加载,在onDestory清除任务和进行缓存的回收
原因:
整个流程调用Glide.with时首先获取到Glide单例中的RequestManagerRetriever,再调用retriever的get(),再他的get方法中创建了一个无界面的Fragment并且注册给Activity,然后将这个Fragment里面的LifeCycle传递给要返回的RequestManager,在这个RequestManager中给LifeCycle注册了一个观察者,这样就完成了生命周期在Activity->Fragment->RequestManager之间的传递;

你可能感兴趣的:(面试,glide,职场和发展)