Glide
会根据页面(Activity、Fragment)的生命周期,适时的开始、取消图片加载。那么问题来了,页面的生命周期事件是如何从页面传递到Glide
中的呢?
Glide.with(Activity)
会调用 RequestManagerRetriver.get(Activity)
返回RequestManager
。在 RequestManagerRetriver.get(Activity)
中, 会 new
出来一个 RequestManagerFragment
,并将其add
到该Activity
对应的FragmentManager
中,通过观察 RequestManagerFragment
的Lifecycle
,进而观察 对应Activity
的 Lifecycle
。
RequestManagerFragment
持有 ActivityFragmentLifecycle
,生命周期事件会下发到ActivityFragmentLifecycle
中,进而被传递到实现了LifecycleListener
接口的 RequestManager
中
@startuml
class RequestManager {
TargetTracker targetTracker
}
class RequestManagerFragment {
ActivityFragmentLifecycle lifecycle
}
class ActivityFragmentLifecycle {
Set<LifecycleListener> lifecycleListeners
}
interface LifecycleListener {
void onStart()
void onStop()
void onDestroy()
}
class TargetTracker {
Set<Target<?>> targets
}
interface Target {
}
LifecycleListener <-- RequestManager: implement
LifecycleListener <-- TargetTracker: implement
LifecycleListener <-- Target: implement
'ActivityFragmentLifecycle --> LifecycleListener: lifecycle event
RequestManagerFragment --> ActivityFragmentLifecycle: lifecycle event
ActivityFragmentLifecycle --> RequestManager: lifecycle event
RequestManager --> TargetTracker: lifecycle event
TargetTracker --> Target: lifecycle event
@enduml
RequestManagerFragment 被新建出来并被添加到 Activity 对应的 Activity 中
@NonNull
private RequestManagerFragment getRequestManagerFragment(
@NonNull final android.app.FragmentManager fm,
@Nullable android.app.Fragment parentHint,
boolean isParentVisible) {
//..
current = new RequestManagerFragment();
//..
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
}
}
return current;
}
RequestManager structure
ActivityFragmentLifecycle
LifecycleListener