Glidle 生命周期-基于3.7.0

1、 Glide.with(context) 中根据context 实现生命周期管理,context 类型如下

public RequestManager get(Context context) {
    if (context == null) {
        throw new IllegalArgumentException("You cannot start a load on a null Context");
    } else {
        if (Util.isOnMainThread() && !(context instanceof Application)) {
            if (context instanceof FragmentActivity) {
                return this.get((FragmentActivity)context);
            }

            if (context instanceof Activity) {
                return this.get((Activity)context);
            }

            if (context instanceof ContextWrapper) {
                return this.get(((ContextWrapper)context).getBaseContext());
            }
        }

        return this.getApplicationManager(context);
    }
}

 Lifecycle 与 Fragment 生命周期绑定

 

public RequestManager get(FragmentActivity activity) {
    if (Util.isOnBackgroundThread()) {
//如果是子线程,默认是getApplicationContext 与应用生命周期一致
        return this.get(activity.getApplicationContext());
    } else {
//传入activity 为例,查看生命周期管理
        assertNotDestroyed(activity);
//activity 与 fragement 绑定
        android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager();
//fm 与 RequestManager 绑定
        return this.supportFragmentGet(activity, fm);
    }
}
RequestManager supportFragmentGet(Context context, android.support.v4.app.FragmentManager fm) {
    SupportRequestManagerFragment current = this.getSupportRequestManagerFragment(fm);
    RequestManager requestManager = current.getRequestManager();
    if (requestManager == null) {
//创建一个RequestManager 对象,将自定义的Lifesycle与Fragment 绑定
        requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());
        current.setRequestManager(requestManager);
    }

    return requestManager;
}
SupportRequestManagerFragment getSupportRequestManagerFragment(android.support.v4.app.FragmentManager fm) {
    SupportRequestManagerFragment current = (SupportRequestManagerFragment)fm.findFragmentByTag("com.bumptech.glide.manager");
    if (current == null) {
        current = (SupportRequestManagerFragment)this.pendingSupportRequestManagerFragments.get(fm);
        if (current == null) {
//创建fragment,添加到集合中,然后移除
            current = new SupportRequestManagerFragment();
            this.pendingSupportRequestManagerFragments.put(fm, current);
            fm.beginTransaction().add(current, "com.bumptech.glide.manager").commitAllowingStateLoss();
            this.handler.obtainMessage(2, fm).sendToTarget();
        }
    }

    return current;
}

public class SupportRequestManagerFragment extends Fragment {
    private RequestManager requestManager;
    private final ActivityFragmentLifecycle lifecycle;
    private final RequestManagerTreeNode requestManagerTreeNode;
    private final HashSet<SupportRequestManagerFragment> childRequestManagerFragments;
    private SupportRequestManagerFragment rootRequestManagerFragment;

    public SupportRequestManagerFragment() {
        this(new ActivityFragmentLifecycle());
    }

    @SuppressLint({"ValidFragment"})
    public SupportRequestManagerFragment(ActivityFragmentLifecycle lifecycle) {
        this.requestManagerTreeNode = new SupportRequestManagerFragment.SupportFragmentRequestManagerTreeNode();
        this.childRequestManagerFragments = new HashSet();
        this.lifecycle = lifecycle;
    }


    ActivityFragmentLifecycle getLifecycle() {
        return this.lifecycle;
    }
...
   public void onStart() {
        super.onStart();
        this.lifecycle.onStart();
    }

    public void onStop() {
        super.onStop();
        this.lifecycle.onStop();
    }

    public void onDestroy() {
        super.onDestroy();
        this.lifecycle.onDestroy();
    }

    public void onLowMemory() {
        super.onLowMemory();
        if (this.requestManager != null) {
            this.requestManager.onLowMemory();
        }

    }

}

注册RequestManager 到 Lifecycle

public class RequestManager implements LifecycleListener {
    private final Context context;
    private final Lifecycle lifecycle;
    private final RequestManagerTreeNode treeNode;
    private final RequestTracker requestTracker;
    private final Glide glide;
    private final RequestManager.OptionsApplier optionsApplier;
    private RequestManager.DefaultOptions options;

    public RequestManager(Context context, Lifecycle lifecycle, RequestManagerTreeNode treeNode) {
        this(context, lifecycle, treeNode, new RequestTracker(), new ConnectivityMonitorFactory());
    }

    RequestManager(Context context, final Lifecycle lifecycle, RequestManagerTreeNode treeNode, RequestTracker requestTracker, ConnectivityMonitorFactory factory) {
        this.context = context.getApplicationContext();
        this.lifecycle = lifecycle;
        this.treeNode = treeNode;
        this.requestTracker = requestTracker;
        this.glide = Glide.get(context);
        this.optionsApplier = new RequestManager.OptionsApplier();
        ConnectivityMonitor connectivityMonitor = factory.build(context, new RequestManager.RequestManagerConnectivityListener(requestTracker));
        if (Util.isOnBackgroundThread()) {
            (new Handler(Looper.getMainLooper())).post(new Runnable() {
                public void run() {
//将当前对象注册在ActivityFragmentLifecycle 中
                    lifecycle.addListener(RequestManager.this);
                }
            });
        } else {
            lifecycle.addListener(this);
        }

        lifecycle.addListener(connectivityMonitor);
    }

。。。

public void onStart() {
    this.resumeRequests();
}

public void onStop() {
    this.pauseRequests();
}

public void onDestroy() {
    this.requestTracker.clearRequests();
}

 

class ActivityFragmentLifecycle implements Lifecycle {
    private final Set<LifecycleListener> lifecycleListeners = Collections.newSetFromMap(new WeakHashMap());
    private boolean isStarted;
    private boolean isDestroyed;

    ActivityFragmentLifecycle() {
    }

    public void addListener(LifecycleListener listener) {
        this.lifecycleListeners.add(listener);
        if (this.isDestroyed) {
            listener.onDestroy();
        } else if (this.isStarted) {
            listener.onStart();
        } else {
            listener.onStop();
        }

    }

    void onStart() {
        this.isStarted = true;
        Iterator i$ = Util.getSnapshot(this.lifecycleListeners).iterator();

        while(i$.hasNext()) {
每个RequestManager对应一个LifecycleListener
            LifecycleListener lifecycleListener = (LifecycleListener)i$.next();
            lifecycleListener.onStart();
        }

    }

    void onStop() {
        this.isStarted = false;
        Iterator i$ = Util.getSnapshot(this.lifecycleListeners).iterator();

        while(i$.hasNext()) {
            LifecycleListener lifecycleListener = (LifecycleListener)i$.next();
            lifecycleListener.onStop();
        }

    }

    void onDestroy() {
        this.isDestroyed = true;
        Iterator i$ = Util.getSnapshot(this.lifecycleListeners).iterator();

        while(i$.hasNext()) {
            LifecycleListener lifecycleListener = (LifecycleListener)i$.next();
            lifecycleListener.onDestroy();
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值