Glide如何自动管理生命周期

2018-05-17  本文已影响0人  緦菍亭芷

Glide管理生命周期还是很有想法的,这种思路值得我们去学习和应用;

特点

1.不用我们自己手动去实现他的接口或者注册;

实现原理

1.通过Fragment依附Activity来监听生命周期变化;
2.本身不关注上层FragmentActivity或者Activity实现;
3.glide通过本身实例化一个Fragment,来添加到FragmentActivity或者Activity,这样只要关注该Fragment就可以知道生命周期变化;
大概画了一个时序图

源码分析

Glide.with()方法参数可以是Activity,FragmentActivity,Fragment等,我们这里与with(@NonNull Activity activity)来分析,其他的类似:

Glide.class

  @NonNull
  public static RequestManager with(@NonNull Activity activity) {
    return getRetriever(activity).get(activity);
  }
RequestManagerRetriever 获得该对象,我们这里不去关心,你只要知道Glide对象实例化的时候会初始化;

RequestManagerRetriever.class

  @SuppressWarnings("deprecation")
  @NonNull
  public RequestManager get(@NonNull Activity activity) {
    if (Util.isOnBackgroundThread()) {ir
      return get(activity.getApplicationContext());
    } else {
      assertNotDestroyed(activity);
      //获取FragmentManager
      android.app.FragmentManager fm = activity.getFragmentManager();
      //重点在这里
      return fragmentGet(
          activity, fm, /*parentHint=*/ null, isActivityVisible(activity));
    }
  }
  @SuppressWarnings({"deprecation", "DeprecatedIsStillUsed"})
  @Deprecated
  @NonNull
  private RequestManager fragmentGet(@NonNull Context context,
      @NonNull android.app.FragmentManager fm,
      @Nullable android.app.Fragment parentHint,
      boolean isParentVisible) {
    //得到我们自己的Fragment
    RequestManagerFragment current = getRequestManagerFragment(fm, parentHint, isParentVisible);
    RequestManager requestManager = current.getRequestManager();
    if (requestManager == null) {
      // TODO(b/27524013): Factor out this Glide.get() call.
      Glide glide = Glide.get(context);
      requestManager =
          factory.build(
              glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);
      current.setRequestManager(requestManager);
    }
    return requestManager;
  }

  @SuppressWarnings("deprecation")
  @NonNull
  private RequestManagerFragment getRequestManagerFragment(
      @NonNull final android.app.FragmentManager fm,
      @Nullable android.app.Fragment parentHint,
      boolean isParentVisible) {
    //获取该FragmentManager是否已经有RequestManagerFragment
    RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
    if (current == null) {
      current = pendingRequestManagerFragments.get(fm);
      if (current == null) {
        current = new RequestManagerFragment();
        current.setParentFragmentHint(parentHint);
        if (isParentVisible) {
          current.getGlideLifecycle().onStart();
        }
        pendingRequestManagerFragments.put(fm, current);
      //添加到上下文,赋予该RequestManagerFragment生命周期
        fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
        //pendingRequestManagerFragments.remove(fm);
        handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
      }
    }
    return current;
  }

到此Glide.with(Activity)该请求已经和传入的activity生命周期绑定;

最后返回一个RequestManager对象,该对象时间在RequestManagerRetriever.supportFragmentGet()方法构造出来

  @NonNull
  private RequestManager supportFragmentGet(
      @NonNull Context context,
      @NonNull FragmentManager fm,
      @Nullable Fragment parentHint,
      boolean isParentVisible) {
    SupportRequestManagerFragment current =
        getSupportRequestManagerFragment(fm, parentHint, isParentVisible);
    RequestManager requestManager = current.getRequestManager();
    if (requestManager == null) {
      // TODO(b/27524013): Factor out this Glide.get() call.
      Glide glide = Glide.get(context);
      //这是就是给RequestManager构造函数传入参数
      requestManager =
          factory.build(
              glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);
      current.setRequestManager(requestManager);
    }
    return requestManager;
  }

RequestManager.class

前面都是add,最后移除在RequestManager.class

  @Override
  public void onDestroy() {
    targetTracker.onDestroy();
    for (Target<?> target : targetTracker.getAll()) {
      clear(target);
    }
    targetTracker.clear();
    requestTracker.clearRequests();
    lifecycle.removeListener(this);
    lifecycle.removeListener(connectivityMonitor);
    mainHandler.removeCallbacks(addSelfToLifecycle);
    glide.unregisterRequestManager(this);
  }

结束!

上一篇下一篇

猜你喜欢

热点阅读