Glide
會根據(jù)頁面(Activity钩蚊、Fragment)的生命周期锻霎,適時的開始、取消圖片加載事富。那么問題來了剑勾,頁面的生命周期事件是如何從頁面?zhèn)鬟f到Glide
中的呢?
Glide.with(Activity)
會調(diào)用 RequestManagerRetriver.get(Activity)
返回RequestManager
赵颅。在 RequestManagerRetriver.get(Activity)
中虽另, 會 new
出來一個 RequestManagerFragment
,并將其add
到該Activity
對應(yīng)的FragmentManager
中饺谬,通過觀察 RequestManagerFragment
的Lifecycle
捂刺,進(jìn)而觀察 對應(yīng)Activity
的 Lifecycle
谣拣。
RequestManagerFragment
持有 ActivityFragmentLifecycle
,生命周期事件會下發(fā)到ActivityFragmentLifecycle
中族展,進(jìn)而被傳遞到實現(xiàn)了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 對應(yīng)的 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