Glide有四个静态的重载方法with(),其内部都通过RequestManagerRetriever相应的get重载方法获取一个RequestManager对象。RequestManagerRetriever提供各种重载方法的好处就是可以将Glide的加载请求与Activity/Fragment的生命周期绑定而自动执行请求,暂停操作。 接下来我们拿Activity参数分析Glide请求如何和绑定生命周期自动请求,暂停,以及销毁。

首先我们进入get()方法

可以看到首先是判断是否在主线程然后返回一个get方法 这个是用来判断是(Content Activity 等等)

如果不在主线程 我们就去assertNotDestroyed 这个方法看下

判断安卓版本 和活动是否被销毁 如果被销毁则抛出异常 然后获取当前参数的FragmentManage 返回一个fragmentGet方法并传入参数

RequestManagerFragment是 继承了Fragment.并且在其生命周期onStart(),onStop(),onDestory(),调用了ActivityFragmentLifecycle 相应的方法,ActivityFragmentLifecycle实现了Lifecycle 接口,在其中通过addListener(LifecycleListener listener)回调相应(LifecycleListener的 onStart(),onStop(),onDestory())周期方法。LifecycleListener是监听生命周期时间接口。 我们再看下一句代码 获得一个RequestManager 的对象

他实现了LifecycleListener,这个监听生命周期的接口 然后判断如果为空 则重新初始化了Glide对象然后通过工厂方法重新创建一个requestManager 我们先去看下Glide.get 这个方法 后面的那个工厂方法先不分析 进入方法一路往下点 我们来到了initializeGlide这个方法

首先是获取ApplicationContext的实例 然后获取GeneratedAppGlideModule(通过反射获得的) 的对象 然后创建GlideModule的一个空集合 然后通过一个方法解析清单文件配置的自定义GlideModule的metadata标签,返回一个GlideModule(这个类稍后分析)集合 因为Glide4.0支持我们通过注解自定义模式,在编译时自动生成需要的代码,所以方法中首先是判断用户有没有自定义模式,有的话需要处理一下。之后就是通过构建者模式创建glide了。

网友评论