源码分析->撕开Glide(1)生命周期管理

源码分析基于 3.7.0
关键字:Glide 生命周期管理

同学们都应该知道,Gilde能很好地跟Activity/Fragment生命周期联动,进而管理Request。具体是怎么管理的呢?我们一起探索下。

Glide.with(this)

关键入口是with方法,这个方法有很多重载的函数,应对不同的场景,我们以Activity为例:
retriever.get(activity),主要是返回RequestManager,RequestManager顾名思义就是对请求的管理,一个Activity对应一个RequestManager,我们跟进这个方法看看;

    public static RequestManager with(Activity activity) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(activity);
    }
RequestManagerRetriever.get

(1)activity.getFragmentManager,获取FragmentManager用来添加RequestManagerFragment;
(2)fragmentGet,获取RequestManager,我们跟进去看看;

    public RequestManager get(Activity activity) {
         ......
         android.app.FragmentManager fm = activity.getFragmentManager();
         return fragmentGet(activity, fm);
    }
RequestManagerRetriever.fragmentGet

(1)getRequestManagerFragment,获取RequestManagerFragment,这是监听Activity生命周期用的,我们跟进方法看看是如何获取Fragment的;
(2)如果该RequestManagerFragment没有绑定RequestManager,则绑定一个新的RequestManager ;
(3)在构造RequestManager,传递了参数current.getLifecycle(),这是ActivityFragmentLifecycle类别,它内部维护了一个监听集合lifecycleListeners,RequestManager把自己添加到这个集合当中,到这里我们大概知道:当Activity生命周期发生变化,RequestManagerFragment得到响应,通过ActivityFragmentLifecycle进而通知所有监听器,这样RequestManager就能感知生命周期变化了;

    RequestManager fragmentGet(Context context, android.app.FragmentManager fm) {
        RequestManagerFragment current = getRequestManagerFragment(fm);
        RequestManager requestManager = current.getRequestManager();
        if (requestManager == null) {
            requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());
            current.setRequestManager(requestManager);
        }
        return requestManager;
    }
RequestManagerRetriever.getRequestManagerFragment

(1)fm.findFragmentByTag(FRAGMENT_TAG),根据名称获取RequestManagerFragment ;
(2)如果(1)返回null,表示该Activity没有添加过RequestManagerFragment,则从pending集合pendingRequestManagerFragments来找,这个集合用来保存即将要加入当前Activity的RequestManagerFragment;
(3)如果(2)返回null,则创建RequestManagerFragment;
(4) fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss(),添加到当前的Activity中;

    RequestManagerFragment getRequestManagerFragment(final android.app.FragmentManager fm) {
        RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
        if (current == null) {
            current = pendingRequestManagerFragments.get(fm);
            if (current == null) {
                current = new RequestManagerFragment();
                pendingRequestManagerFragments.put(fm, current);
                fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
                handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
            }
        }
        return current;
    }

到这里,我们知道RequestManagerFragment 被加到当前Activity当中,接下来看它是怎么工作的。

RequestManagerFragment

(1)重点关注以下内容;
(2)RequestManagerFragment 继承android.app .Fragment ,并且没有添加任何布局文件,所以它是个空白的;
(3)包含一个ActivityFragmentLifecycle类型的成员变量lifecycle,看名字知道它会做跟生命周期相关的内容;
(4)重写了onStart、onStop、onDestroy,分别对应Activity可见、不可见、销毁,并且分发了事件。
(5)在分析RequestManagerRetriever.fragmentGet时候,我们知道构建RequestManager会传入RequestManagerFragment 的成员变量lifecycle,构造方法内部会把自身添加到lifecycle集合当中,所以RequestManager 可以监听onStart、onStop、onDestroy;

ActivityFragmentLifecycle

(1)实现了Lifecycle ,注意它不是Jetpack的Lifecycle,这是Glide自定义的;
(2)维护监听器lifecycleListeners ;
(3)addListener,可以注册监听器,注意注册完监听器以后,会立即通知监听器,当前的状态;
(4)onStart、onStop、onDestroy方法将通知所有监听器;

class ActivityFragmentLifecycle implements Lifecycle {
    private final Set lifecycleListeners =
            Collections.newSetFromMap(new WeakHashMap());
    ······
    @Override
    public void addListener(LifecycleListener listener) {
        lifecycleListeners.add(listener);

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

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

RequestManager

(1)在构造方法当中,会把自身添加到lifecycle当中,这个lifecycle是RequestManagerFragment的成员变量;
(2)onStart,当前Activity可见,将回调onStart,遍历RequestManager管理的Request,让没有完成或失败的Request加载图片,注意如果是在Activity onStart之后使用的Glide将收不到onStart回调;
(3)onStop,当前Activity不可见,将回调onStop,遍历RequestManager管理的Request,把状态改成Status.PAUSED,把Request加入的pending队列;
(4)onDestory,当前Activity销毁,将回调onDestory,遍历RequestManager管理的Request,把状态改成Status.CLEARED并回收所占用的资源,并清空pending队列;

public class RequestManager implements LifecycleListener {
    private final Lifecycle lifecycle;
    private final RequestTracker requestTracker;
    ······
    RequestManager(Context context, final Lifecycle lifecycle, RequestManagerTreeNode treeNode,
            RequestTracker requestTracker, ConnectivityMonitorFactory factory) {
      ......
       lifecycle.addListener(this);
      ......
    }
    @Override
    public void onStart() {
        // onStart might not be called because this object may be created after the fragment/activity's onStart method.
        resumeRequests();
    }
    /**
     * Lifecycle callback that unregisters for connectivity events (if the android.permission.ACCESS_NETWORK_STATE
     * permission is present) and pauses in progress loads.
     */
    @Override
    public void onStop() {
        pauseRequests();
    }

    /**
     * Lifecycle callback that cancels all in progress requests and clears and recycles resources for all completed
     * requests.
     */
    @Override
    public void onDestroy() {
        requestTracker.clearRequests();
    }
    ......
}

RequestManager.RequestManagerConnectivityListener.onConnectivityChanged

(1)当有网络,isConnected为true,则requestTracker.restartRequests重启图片加载;
(2)网络监听是在DefaultConnectivityMonitor里面实现的,它也是监听RequestManagerFragment的生命周期进而注册、注销广播,有兴趣的同学可以跟进去看看;

    private static class RequestManagerConnectivityListener implements ConnectivityMonitor.ConnectivityListener {
       ······
        @Override
        public void onConnectivityChanged(boolean isConnected) {
            if (isConnected) {
                requestTracker.restartRequests();
            }
        }
    }

RequestManagerRetriever.get

(1)重点看RequestManagerRetriever.get方法实现;
(2)同样是通过fragmentGet来构建RequestManagerFragment以及RequestManager,不同的是 FragmentManager 是当前Fragment的ChildFragmentManager,这是在Fragment里添加子Fragmet用的;

    public RequestManager get(android.app.Fragment fragment) {
              ······
 else {
            android.app.FragmentManager fm = fragment.getChildFragmentManager();
            return fragmentGet(fragment.getActivity(), fm);
        }
    }

RequestManager.get

(1)重点还是看RequestManager.get方法实现;
(2)因为传入的context,所以调用以下重载方法,
(2)我们跟进getApplicationManager看看;

    public RequestManager get(Context context) {
        ······
        return getApplicationManager(context);
    }
RequestManagerRetriever.getApplicationManager

(1)这里还用到了单例模式,其实也是对的,一个Application对应一个applicationManager ;
(2)在构建RequestManager的时候有些不同,传入的是ApplicationLifecycle;

    private RequestManager getApplicationManager(Context context) {
        // Either an application context or we're on a background thread.
        if (applicationManager == null) {
            synchronized (this) {
                if (applicationManager == null) {
                    applicationManager = new RequestManager(context.getApplicationContext(),
                            new ApplicationLifecycle(), new EmptyRequestManagerTreeNode());
                }
            }
        }
        return applicationManager;
    }
看到这里大家可能有些明白了,如果是在Glide.with传入applicationContext,那么将不会构建RequestManagerFragment,RequestManager 将添加到ApplicationLifecycle当中,这样将监听不到onStart、onStop、onDestory。仔细想想,其实也不需要,因为传入applicationContext,表示Reuest的生命周期跟Application一样长
总结

(1)Glide通过给Activity添加空白的Fragment来实现生命周期监听;
(2)主要监听onStart、onStop、onDestroy方法;
(3)RequestManager也注册了网络广播监听;
(4)前面多次提到的pending队列,该队列维护Request的强引用,当他们处于暂停或者没有开始加载图片之前,保证请求不会被回收;
(5)如果是在Fragment加载图片,是将空白的Fragment当成子Fragment添加进去;
(6)如果是applicationContext,则不会任何生命周期联动的处理;

以上分析有不对的地方,请指出,互相学习,谢谢哦!

你可能感兴趣的:(源码分析->撕开Glide(1)生命周期管理)