谈谈你对Glide生命周期的理解(腾讯)
这道题想考察什么?
- 是否了解Glide生命周期与真实场景使用,是否熟悉Glide生命周期在工作中的表现是什么?
考察的知识点
- Glide生命周期的概念在项目中使用与基本知识
考生应该如何回答
1.说说Glide生命周期?
答:
首先找到Fragment,也就是RequestManagerFragment,内部创建的无UI的Fargment。它在RequestManagerRetriever类的getRequestManagerFragment()被调用。相关源码
final Map<android.app.FragmentManager, RequestManagerFragment> pendingRequestManagerFragments =
new HashMap<>();
private RequestManagerFragment getRequestManagerFragment(
@NonNull final android.app.FragmentManager fm,
@Nullable android.app.Fragment parentHint,
boolean isParentVisible) {
RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);//获取Fragment
if (current == null) {
current = pendingRequestManagerFragments.get(fm);
if (current == null) {
current = new RequestManagerFragment();
current.setParentFragmentHint(parentHint);
if (isParentVisible) {
current.getGlideLifecycle().onStart();
}
pendingRequestManagerFragments.put(fm, current);//保存到map集合
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();//添加到Actvity中
handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
}
}
return current;
}
这部分代码涉及FragmentManager的使用,忘了怎么使用的朋友可以去复习一下。代码不难理解,就是一个目的,得到Fragment并且返回这个Fragment。先通过findFragmentByTag获取,如果为null,则会从pendingRequestManagerFragments这个Map集合去获取,如果还是为null,则直接new 一个Fragment,并且保存到pendingRequestManagerFragments以及添加到Activity中。这部分的代码就是Fragment和Actvity关联上了,这样就可以通过Fragment得知当前Activty的生命周期。追踪RequestManagerFragment,看看它的生命周期里面做了什么操作,源码如下。
private final ActivityFragmentLifecycle 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();
}
Fragment的的生命周期里(我们只关注贴出代码的三个生命周期),ActivityFragmentLifecycle类都调用了相同名字的方法,接下来看看ActivityFragmentLifecycle的相应方法里面有什么操作。
private final Set<LifecycleListener> lifecycleListeners =
Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>());
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();
}
}
相应的方法里都调用了LifecycleListener的相关方法,LifecycleListener是一个接口,作用在源码里写得很清楚:An interface for listener to lifecycle events.
,监听生命周期的接口。既然是个接口,就找出实现它的类,实现LifecycleListener的类是RequestManager。RequestManager类,它实现了LifecycleListener接口,三个方法里面的内容如下。
/**
* Lifecycle callback that registers for connectivity events (if the
* android.permission.ACCESS_NETWORK_STATE permission is present) and restarts failed or paused
* requests.
*/
@Override
public synchronized void onStart() {
resumeRequests();
targetTracker.onStart();
}
/**
* Lifecycle callback that unregisters for connectivity events (if the
* android.permission.ACCESS_NETWORK_STATE permission is present) and pauses in progress loads.
*/
@Override
public synchronized void onStop() {
pauseRequests();
targetTracker.onStop();
}
/**
* Lifecycle callback that cancels all in progress requests and clears and recycles resources for
* all completed requests.
*/
@Override
public synchronized 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);
}
最后
我整理了一套Android面试题合集,除了以上面试题,还包含【Java 基础、集合、多线程、虚拟机、反射、泛型、并发编程、Android四大组件、异步任务和消息机制、UI绘制、性能调优、SDN、第三方框架、设计模式、Kotlin、计算机网络、系统启动流程、Dart、Flutter、算法和数据结构、NDK、H.264、H.265.音频编解码、FFmpeg、OpenMax、OpenCV、OpenGL ES】