Glide.with(this).load(url).into(imageview)
Glide绑定fragment、Activity的生命周期是在with方法里面执行的,with可以为Fragment,Activity,FragmentActivity,Context,View,如果是Activity 或者fragment直接调用对应的get方法,如果为Context或者View则判断该Context的类型,如何为Fragment,Activity则创建换一个默认的fragment,返回 RequestManager,否则不会创建默认的fragment,直接返回RequestManager。下面直接看如何创建空的fragment,来绑定生命周期。
Fragment的get方法为例:
public RequestManager get(@NonNull Fragment fragment) {
if (Util.isOnBackgroundThread()) {
return get(fragment.getContext().getApplicationContext());
} else {
//根据activity.getChildFragmentManager();获取fm
FragmentManager fm = fragment.getChildFragmentManager();
return supportFragmentGet(fragment.getContext(), fm, fragment, fragment.isVisible());
}
}
不管是Activity、Fragment、FragmentActivity都会创建FragmentManager,然后调用fragmentGet/supportFragmentGet方法,这个方法才是生成管理生命周期的关键。在添加的时候需要注意是使用fm.beginTransaction().add方法添加到当前的fragment或者Activity上去的,然后在commitAllowingStateLoss发送到主线程。
@NonNull
private RequestManager supportFragmentGet(
@NonNull Context context,
@NonNull FragmentManager fm,
@Nullable Fragment parentHint,
boolean isParentVisible) {
//生成SupportRequestManagerFragment
SupportRequestManagerFragment current =getSupportRequestManagerFragment(fm, parentHint, isParentVisible);
//获取请求管理类
RequestManager requestManager = current.getRequestManager();
if (requestManager == null) {
//如果请求管理类为空,则生成该对象
Glide glide = Glide.get(context);
requestManager =factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);
current.setRequestManager(requestManager);
}
return requestManager;
}
//生成SupportRequestManagerFragment
private SupportRequestManagerFragment getSupportRequestManagerFragment(
@NonNull final FragmentManager fm, @Nullable Fragment parentHint, boolean isParentVisible) {
//根据TAG找到current,这里的TAG是一个固定的值,也就是说只有一个fragment
SupportRequestManagerFragment current =(SupportRequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
if (current == null) {
//如果为空
current = pendingSupportRequestManagerFragments.get(fm);
if (current == null) {
//首次进来没有,则会创建
current = new SupportRequestManagerFragment();
current.setParentFragmentHint(parentHint);
if (isParentVisible) {
current.getGlideLifecycle().onStart();
}
//放到HashMap里面,以便于下次进来取
pendingSupportRequestManagerFragments.put(fm, current);
//注意这里的add方法,添加的是一个空的fragment,没有id,只有一个TAG,添加到宿主fragment上面
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
//发送到主线程
handler.obtainMessage(ID_REMOVE_SUPPORT_FRAGMENT_MANAGER, fm).sendToTarget();
}
}
return current;
}
经过以上步骤空的SupportRequestManagerFragment就生成了,详细过程看备注。
RequestManagerFragment是一个空的fragment,没有布局和View操作,仅仅用来监听生命周期。
public RequestManagerFragment() {
this(new ActivityFragmentLifecycle());
}
RequestManagerFragment(@NonNull ActivityFragmentLifecycle lifecycle) {
this.lifecycle = lifecycle;
}
1、ActivityFragmentLifecycle
在RequestManagerFragment无参数的构造方法里面会创建ActivityFragmentLifecycle对象,他是一个生命周期的回调监听,实现了Lifecycle接口。负责注册和移除LifecycleListener。除此之外,还包含onStart,onStop,onDestroy三个方法,在这三个方法里面遍历Set获得对应的LifecycleListener的对应方法,这里的LifecycleListener其实就是RequestManager,即调RequestManager的三个方法。
class ActivityFragmentLifecycle implements Lifecycle {
private final Set lifecycleListeners =
Collections.newSetFromMap(new WeakHashMap());
private boolean isStarted;
private boolean isDestroyed;
//注册监听
@Override
public void addListener(@NonNull LifecycleListener listener) {
lifecycleListeners.add(listener);
if (isDestroyed) {
listener.onDestroy();
} else if (isStarted) {
listener.onStart();
} else {
listener.onStop();
}
}
//移除监听
@Override
public void removeListener(@NonNull 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();
}
}
}
2、RequestManagerFragment生命周期传递
当创建RequestManagerFragment结束后,当前页面加载的时候生命周期传递到RequestManagerFragment,就会执行对象的生命周期的方法,当执行onStart时候,调用ActivityFragmentLifecycle的onStart,当执行onStop时候,调用ActivityFragmentLifecycle的onStop……,这样ActivityFragmentLifecycle就会调用lifecycleListener.onStart()等方法,把回调交给了ActivityFragmentLifecycle。
3、RequestManager生命周期内的请求管理
with方法再创建完RequestManagerFragment之后会返回fragmentGet()/supportFragmentGet()该方法直接返回了一个RequestManager,看看第一部分的supportFragmentGet代码:
private RequestManager supportFragmentGet(
…………………………………………………………
//获取请求管理类
RequestManager requestManager = current.getRequestManager();
if (requestManager == null) {
//如果请求管理类为空,则生成该对象
Glide glide = Glide.get(context);
requestManager =factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);
current.setRequestManager(requestManager);
}
return requestManager;
}
RequestManager实现了LifecycleListener接口,在构造方法中传递了current.getGlideLifecycle()参数,该参数就是ActivityFragmentLifecycle,随之立即调用ActivityFragmentLifecycle的addListener方法进行注册,这样ActivityFragmentLifecycle生命周期管理类执行对应生命周期的时候就会调用RequestManager中的对应方法。对应的onStart就会开启网络请求,和设置ImageView监听到ImageView的onStart方法,onStart方法就这样监听到了。
public class RequestManager implements LifecycleListener, ……{
@Override
public synchronized void onStart() {
//发起请求
resumeRequests();
//View的Target回调
targetTracker.onStart();
}
@Override
public synchronized void onStop() {
//暂停请求
pauseRequests();
//View的Target回调
targetTracker.onStop();
}
@Override
public synchronized void onDestroy() {
销毁请求
targetTracker.onDestroy();
//View的Target回调
for (Target> target : targetTracker.getAll()) {
clear(target);
}
//清空回调信息
targetTracker.clear();
requestTracker.clearRequests();
lifecycle.removeListener(this);
lifecycle.removeListener(connectivityMonitor);
mainHandler.removeCallbacks(addSelfToLifecycle);
glide.unregisterRequestManager(this);
}
}
这样不用手动回调onStart,onStop,onDestroy,而是使用RequestManagerFragment回调巧妙的管理了请求的发起和销毁等一系列动作,更加节省内存,还避免了内存泄漏问题。
流程图如下: