Glide 源码解析 之 生命周期传递

Glide会根据页面(Activity、Fragment)的生命周期,适时的开始、取消图片加载。那么问题来了,页面的生命周期事件是如何从页面传递到Glide中的呢?

Glide.with(Activity)会调用 RequestManagerRetriver.get(Activity) 返回RequestManager。在 RequestManagerRetriver.get(Activity) 中, 会 new 出来一个 RequestManagerFragment,并将其add到该Activity对应的FragmentManager中,通过观察 RequestManagerFragmentLifecycle,进而观察 对应ActivityLifecycle
RequestManagerFragment 持有 ActivityFragmentLifecycle,生命周期事件会下发到ActivityFragmentLifecycle中,进而被传递到实现了LifecycleListener 接口的 RequestManager

Lifecycle event spread logic
@startuml

class RequestManager {
    TargetTracker targetTracker
}

class RequestManagerFragment {
    ActivityFragmentLifecycle lifecycle
}

class ActivityFragmentLifecycle {
    Set lifecycleListeners
}

interface LifecycleListener {
    void onStart()
    void onStop()
    void onDestroy()
}

class TargetTracker {
    Set> 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


RequestManager structure

RequestManagerFragment

ActivityFragmentLifecycle


ActivityFragmentLifecycle

LifecycleListener
LifecycleListener

你可能感兴趣的:(Glide 源码解析 之 生命周期传递)