Glide(一)生命周期绑定

  • Glide 的解耦方式很独特。
  • 相关文章:Fragment 如何跟Activity的生命周期进行绑定
  • 1.1 Glide.with()
Glide.with(FragmentActivity activity)
// RequestManager with(FragmentActivity activity)
public static RequestManager with(Activity activity) {
    return getRetriever(activity).get(activity);
}
public RequestManager get(Activity activity) {
    if (Util.isOnBackgroundThread()) {
      return get(activity.getApplicationContext());
    } else {
      assertNotDestroyed(activity);
      // 获取当前Activity的FragmentManager,
      // 后续用于新见Fragment并绑定到当前FragmentManager,从而获取Fragment的生命周期(同步Activity的生命周期)
      android.app.FragmentManager fm = activity.getFragmentManager();
      return fragmentGet(activity, fm, null /*parentHint*/);
    }
  }
  • 1.2 这里主要看 RequestManagerFragment
private RequestManager fragmentGet(Context context, android.app.FragmentManager fm,
      android.app.Fragment parentHint) {
    // a.绑定 RequestManagerFragment 至 FragmentManager ,使RequestManagerFragment 生命周期同步 Activity
    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);
      requestManager =
          // b. 获取一个RequestManager 实例并给当前RequestManagerFragment
          factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode());
      current.setRequestManager(requestManager);
    }
    return requestManager;
  }
  • 1.3 绑定 RequestManagerFragment 至 FragmentManager,当前RequestManagerFragment已经有了其生命周期,看下如何实现
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  RequestManagerFragment getRequestManagerFragment(
      final android.app.FragmentManager fm, android.app.Fragment parentHint) {
     //a.根据TAG id获取RequestManagerFragment实例
    RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
    if (current == null) {
      // b. 如果a没获取实例,从内存的map内获取
      current = pendingRequestManagerFragments.get(fm);
      if (current == null) {
        // c. 以上a,b 都没有获取实例,则最后new一个,并put 到内存 map内
        current = new RequestManagerFragment();
        current.setParentFragmentHint(parentHint);
        pendingRequestManagerFragments.put(fm, current);
        // fragment 与 FragmentManager 绑定,那么 RequestManagerFragment 有了生命周期
        fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
        handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
      }
    }
    return current;
  }
  • 1.4 以上步骤中参考 1.2中b操作使得RequestManagerFragment中有RequestManager,RequestManager在构造中将自身添加到Lifecycle中
RequestManager(
      Glide glide,
      Lifecycle lifecycle,
      RequestManagerTreeNode treeNode,
      RequestTracker requestTracker,
      ConnectivityMonitorFactory factory) {
    this.glide = glide;
    this.lifecycle = lifecycle;
    this.treeNode = treeNode;
    this.requestTracker = requestTracker;

    ...
    if (Util.isOnBackgroundThread()) { // 非主线程
      mainHandler.post(addSelfToLifecycle);
    } else { // 主线程
      lifecycle.addListener(this);
    }
    lifecycle.addListener(connectivityMonitor);

    setRequestOptions(glide.getGlideContext().getDefaultRequestOptions());

    glide.registerRequestManager(this);
  }

  private final Runnable addSelfToLifecycle = new Runnable() {
    @Override
    public void run() {
      lifecycle.addListener(RequestManager.this);
    }
  };
  • 2.1 Fragment 生命周期的传递
public RequestManagerFragment() {
    // 构造时初始化ActivityFragmentLifecycle
    this(new ActivityFragmentLifecycle());
}

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

// 以下为生命周期的透传
 @Override
  public void onStart() {
    super.onStart();
    lifecycle.onStart();
  }

  @Override
  public void onStop() {
    super.onStop();
    lifecycle.onStop();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    lifecycle.onDestroy();
    unregisterFragmentWithRoot();
  }
  • 2.2 回调所有生命周期的接口
    • RequestManagerFragment 下回调所有注册的 RequestManager
@Override
  public void removeListener(LifecycleListener listener) {
    lifecycleListeners.remove(listener);
  }

  void onStart() {
    isStarted = true;
    for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
      lifecycleListener.onStart();
    }
  }

  void onStop() {
    isStarted = false;
    for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
      lifecycleListener.onStop();
    }
  }

  void onDestroy() {
    isDestroyed = true;
    for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
      lifecycleListener.onDestroy();
    }
  }
}

总结

  • 1.通过获取 Activity 的FragmentManager,绑定一个空RequestManagerFragment,以同步Activity生命周期
  • 2.RequestManagerFragment 中初始化LifecycleListener
  • 3.初始化RequestManagerFragment,同事初始化RequestManager,并注册至LifecycleListener
  • 4.Activity 触发生命周期时,回调 Fragment,并透传至LifecycleListener,LifecycleListener会回调所有向其注册过的RequestManager
  • 综上所得解耦新技能。奉上时序图

你可能感兴趣的:(Android,框架)