1.Glide.with

public class Glide implements ComponentCallbacks2 {
    public static RequestManager with(@NonNull FragmentActivity activity) {
        return getRetriever(activity).get(activity);
    }
}

2.RequestManagerRetriever.get

public class RequestManagerRetriever implements Handler.Callback {
    public RequestManager get(@NonNull FragmentActivity activity) {
        if (Util.isOnBackgroundThread()) {//在后台线程
            return get(activity.getApplicationContext());
        }
        assertNotDestroyed(activity);
        frameWaiter.registerSelf(activity);
        boolean isActivityVisible = isActivityVisible(activity);
        Glide glide = Glide.get(activity.getApplicationContext());
        return lifecycleRequestManagerRetriever.getOrCreate(
            activity,
            glide,
            activity.getLifecycle(),
            activity.getSupportFragmentManager(),
            isActivityVisible);
    }
}

3.LifecycleRequestManagerRetriever.getOrCreate

final class LifecycleRequestManagerRetriever {
    RequestManager getOrCreate(
      Context context,
      Glide glide,
      final Lifecycle lifecycle,
      FragmentManager childFragmentManager,
      boolean isParentVisible) {
        //一个Lifecycle对应一个RequestManager
        RequestManager result = getOnly(lifecycle);
        if (result == null) {//resultnull,才会去创建LifecycleLifecycle
            LifecycleLifecycle glideLifecycle = new LifecycleLifecycle(lifecycle);
        }
    }
}

4.LifecycleLifecycle

final class LifecycleLifecycle implements Lifecycle, LifecycleObserver {
    @NonNull
    private final Set<LifecycleListener> lifecycleListeners = new 
                                             HashSet<LifecycleListener>();

    @NonNull private final androidx.lifecycle.Lifecycle lifecycle;

    LifecycleLifecycle(androidx.lifecycle.Lifecycle lifecycle) {
        //这里activity.getLifecycle()或者fragment.getLifecycle(),所以
        //这里相当于设置了Activity或者Fragment的生命周期监听。
        this.lifecycle = lifecycle;
        lifecycle.addObserver(this); //给Lifecycle注册LifecycleObserver 
    }

    @OnLifecycleEvent(Event.ON_START)
    public void onStart(@NonNull LifecycleOwner owner) {
        for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) 
              {
            lifecycleListener.onStart(); //Activity或者Fragment的onStart的时候执行
        }
    }

    @OnLifecycleEvent(Event.ON_STOP)
    public void onStop(@NonNull LifecycleOwner owner) {
        for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) 
          {
            lifecycleListener.onStop(); //Activity或者Fragment的onStop时候执行
        }
    }

    @OnLifecycleEvent(Event.ON_DESTROY)
    public void onDestroy(@NonNull LifecycleOwner owner) {
        for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) 
               {
            lifecycleListener.onDestroy();
        }
        owner.getLifecycle().removeObserver(this); //移除生命周期监听
    }

    @Override
    public void addListener(@NonNull LifecycleListener listener) {
        lifecycleListeners.add(listener); //添加自定义的LifecycleListener 

        if (lifecycle.getCurrentState() == State.DESTROYED) {
            listener.onDestroy();
        } else if (lifecycle.getCurrentState().isAtLeast(State.STARTED)) {
            listener.onStart();
        } else {
            listener.onStop();
        }
    }

    @Override
    public void removeListener(@NonNull LifecycleListener listener) {
        lifecycleListeners.remove(listener);//移除自定义的LifecycleListener 
    }
}

5.总结

所以在调用Glide.with时候,就给Activity或者fragment添加了生命周期监听observer

原文地址:https://blog.csdn.net/lostfish123/article/details/134795866

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_44866.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注