Glide原理--生命周期绑定

Gilde 的使用

最基础的使用

Glide.with(context).load(url).into(imageView)

with方法

Glide.with(context)
with 重载方法
with(android.app.Activity)
with(android.app.Fragment)
with(android.support.v4.app.Fragment)
with(android.support.v4.app.FragmentActivity)
with(android.view)

with 方法返回的都是一个RequestManager对象,RequestManager 是管理请求的

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

get 方法 根据不同的context 返回不同的RequestManager

public RequestManager get(Context context) {
     
    if (context == null) {
     
        throw new IllegalArgumentException("You cannot start a load on a null Context");
    } else if (Util.isOnMainThread() && !(context instanceof Application)) {
     
        if (context instanceof FragmentActivity) {
     
            return get((FragmentActivity) context);
        } else if (context instanceof Activity) {
     
            return get((Activity) context);
        } else if (context instanceof ContextWrapper) {
     
            return get(((ContextWrapper) context).getBaseContext());
        }
    }

    return getApplicationManager(context);
}

如果context是activity ,继续看

public RequestManager get(@NonNull Activity activity) {
     
  if (Util.isOnBackgroundThread()) {
     
    // 如果不是主线程,则context对象转为ApplicationContext,然后也不做绑定生命周期的操作,
    // 图片加载的生命周期默认为Application的生命周期
    return get(activity.getApplicationContext());
  } else {
     
    assertNotDestroyed(activity);
    //(1)
    android.app.FragmentManager fm = activity.getFragmentManager();
    return fragmentGet(activity, fm, null /*parentHint*/);
  }
}
  • (1)处通过activity 获取了FragmentManager 并传给了fragmentGet方法
    fragmentGet 方法
private RequestManager fragmentGet(@NonNull Context context,
    @NonNull android.app.FragmentManager fm,
    @Nullable android.app.Fragment parentHint) {
     
    //(1)
  RequestManagerFragment current = getRequestManagerFragment(fm, parentHint);
  RequestManager requestManager = current.getRequestManager();
  ......
  return requestManager;
}
  • (1)处 获取到一个无UI的Fragment,用来绑定Activity从而实现生命周期同步
创建Fragment 的原因
  • 因为glide无法直接获取activity的生命周期,通过activity的FragmentManager中加入一个隐藏的fragment,因为fragment与传入的activity生命周期一致,所以只要监听这个RequestManagerFragment就能实现生命周期管理

接下来 getRequestManagerFragment 创建fragment

RequestManagerFragment getRequestManagerFragment(
    @NonNull final android.app.FragmentManager fm, @Nullable android.app.Fragment parentHint) {
     
//(1)
  RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
  //(2)
  if (current == null) {
     
    current = pendingRequestManagerFragments.get(fm);
    if (current == null) {
     //(3)
      current = new RequestManagerFragment();
      ......
    }
  }
  return current;
} 
  • (1)处 这个Fragment是个无UI的Fragment,可以通过findFragmentByTag(FRAGMENT_TAG)去查找这个RequestManagerFragment
  • (2)处 如果第一次进来,current就是null,如果已经创建过,可以通过fm对象找到已有的fragment对象,如果再没有(3)就创建一个RequestManagerFragment

接下来RequestManagerFragment 的构造方法

public class SupportRequestManagerFragment extends Fragment {
     
  
  public SupportRequestManagerFragment() {
     
   //(1)
    this(new ActivityFragmentLifecycle());
  }

  @SuppressLint("ValidFragment")
  public SupportRequestManagerFragment(ActivityFragmentLifecycle lifecycle) {
     
    //(2)
    this.lifecycle = lifecycle;
  }

  ActivityFragmentLifecycle getGlideLifecycle() {
     
    return lifecycle;
  }

  @Override
  public void onStart() {
     
    super.onStart();
    lifecycle.onStart();//(3)
  }

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

  @Override
  public void onDestroy() {
     
    super.onDestroy();
    lifecycle.onDestroy();//(5)
  }
}
  • (1)处 初始化Fragment的时候也顺便初始化了ActivityFragmentLifecycle
  • (2)处 lifecycle赋值 ActivityFragmentLifecycle
  • (3)(4)(5) 处都是fragement 生命周期的回调方法,在生命周期方法中调用ActivityFragmentLifecycle 的生命周期方法

看下ActivityFragmentLifecycle

class ActivityFragmentLifecycle implements Lifecycle {
     
  
  @Override
  public void addListener(LifecycleListener listener) {
     
    lifecycleListeners.add(listener);

    if (isDestroyed) {
     
      listener.onDestroy();
    } else if (isStarted) {
     
      listener.onStart();
    } else {
     
      listener.onStop();
    }
  }

  @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();
    }
  }
}

public interface Lifecycle {
     
  void addListener(LifecycleListener listener);
  void removeListener(LifecycleListener listener);
}
  • ActivityFragmentLifecycle 实现了Lifecycle
  • 对外暴露一个addListener方法,可以监听生命周期的变化
  • ActivityFragmentLifecycle 中的生命周期方法又调用了传递进来的lifecycleListener 的生命周期方法

看下哪里调用了addListener ,在开始的fragmentGet中

private RequestManager fragmentGet(@NonNull Context context,
    @NonNull android.app.FragmentManager fm,
    @Nullable android.app.Fragment parentHint) {
     
  RequestManagerFragment current = getRequestManagerFragment(fm, parentHint);
  RequestManager requestManager = current.getRequestManager();
  if (requestManager == null) {
     
    Glide glide = Glide.get(context);
    //(1)
    requestManager = factory.build(
            glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);
    current.setRequestManager(requestManager);
  }
  return requestManager;
}
  • (1) 处 传入的current.getGlideLifecycle() 就是Lifecycle ,而lifecycle正是ActivityFragmentLifecycle。

RequestManagerFactory

public interface RequestManagerFactory {
     
  @NonNull
  RequestManager build(
      @NonNull Glide glide,
      @NonNull Lifecycle lifecycle,
      @NonNull RequestManagerTreeNode requestManagerTreeNode,
      @NonNull Context context);
}

private static final RequestManagerFactory DEFAULT_FACTORY = new RequestManagerFactory() {
     
  @NonNull
  @Override
  public RequestManager build(@NonNull Glide glide, @NonNull Lifecycle lifecycle,
      @NonNull RequestManagerTreeNode requestManagerTreeNode, @NonNull Context context) {
     
    // (1)
    return new RequestManager(glide, lifecycle, requestManagerTreeNode, context);
  }
};
  • (1) 处 初始化了RequestManager

看下 RequestManager

public class RequestManager implements LifecycleListener {
     

  RequestManager(
      Glide glide,
      Lifecycle lifecycle,
      RequestManagerTreeNode treeNode,
      RequestTracker requestTracker,
      ConnectivityMonitorFactory factory,
      Context context) {
     

    this.glide = glide;
    this.lifecycle = lifecycle;
    this.treeNode = treeNode;
    this.requestTracker = requestTracker;
    this.context = context;

    if (Util.isOnBackgroundThread()) {
     
      mainHandler.post(addSelfToLifecycle);
    } else {
     
      // (1)
      lifecycle.addListener(this);
    }
  }
  ...
  @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);
  }
}
  • (1)处 调用lifecycle.addListener(this); 实现了与RequestManagerFragment生命周期的绑定

类之间关系图:

Glide原理--生命周期绑定_第1张图片

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