Glide手写实现之生命周期关联

系列文章

Glide手写实现之资源封装
Glide手写实现之活动缓存
Glide手写实现之内存缓存
Glide手写实现之磁盘缓存
Glide手写实现之生命周期关联
Glide手写实现之网络图片加载实现
Glide手写实现之复用池

生命周期关联

Glide根据传入的上下文环境,比如Activity,FragmentActivity,关联到与之对应的生命周期回调方法。原理是在Activity或者FragmentActivity中添加一个空的Fragment,从而与Activity或者FragmentActivity的生命周期关联。但是传入Context的话, 没法进行生命周期的关联。

Glide

public class Glide {

    RequestManagerRetriver retriver;

    public Glide(RequestManagerRetriver retriver) {
        this.retriver = retriver;
    }

    public static RequestManager with(FragmentActivity fragmentActivity) {
        return getRetriever(fragmentActivity).get(fragmentActivity);
    }

    public static RequestManager with(Activity activity) {
        return getRetriever(activity).get(activity);
    }

    public static RequestManager with(Context mContext) {
        return getRetriever(mContext).get(mContext);
    }

    /**
     * RequestManager有我们的RequestManagerRetriver去创建的
     * @return
     */
    public static RequestManagerRetriver getRetriever(Context context) {
        return Glide.get(context).getRetriver();
    }

    // Glide 是 new出来的 -- 转变
    public static Glide get(Context context) {
        return new GlideBuilder().build();
    }

    /**
     * RequestManagerRetriver
     * @return
     */
    public RequestManagerRetriver getRetriver() {
        return retriver;
    }
}

public static Glide get(Context context) 这个方法将static的调用转变成非static的调用。

GlideBuilder

public class GlideBuilder {

    /**
     * 创建Glide
     * @return
     */
    public Glide build() {
        RequestManagerRetriver requestManagerRetriver = new RequestManagerRetriver();
        Glide glide = new Glide(requestManagerRetriver);
        return glide;
    }
}

RequestManager

public class RequestManager {

    private final String TAG = RequestManager.class.getSimpleName();

    private final String FRAGMENT_ACTIVITY_NAME = "Fragment_Activity_NAME";
    private final String ACTIVITY_NAME = "activity_name";
    private final int NEXT_HANDLER_MSG = 995465;

    private Context requestManagerContext;
    private static RequestTargetEngine requestTargetEngine;

    // 构造代码块,不用再所有的构造方法里面去实例化了,统一的去写
    {
        if (requestTargetEngine == null) {
            requestTargetEngine = new RequestTargetEngine();
        }
    }

    /**
     * 可以管理生命周期 - FragmentActivity是有生命周期方法的(Fragment)
     * @param fragmentActivity
     */
    FragmentActivity fragmentActivity;
    public RequestManager(FragmentActivity fragmentActivity) {
        this.requestManagerContext = fragmentActivity;
        this.fragmentActivity = fragmentActivity;

        // 拿到Fragment
        FragmentManager supportFragmentManager = fragmentActivity.getSupportFragmentManager();
        Fragment fragment = supportFragmentManager.findFragmentByTag(FRAGMENT_ACTIVITY_NAME);
        if (null == fragment) { // 如果等于null,就要去创建Fragment
            fragment = new FragmentActivityFragmentManager(requestTargetEngine); // Fragment的生命周期与requestTargetEngine关联起来了
            // 添加到 supportFragmentManager
            supportFragmentManager.beginTransaction().add(fragment, FRAGMENT_ACTIVITY_NAME).commitAllowingStateLoss(); // 提交
        }

        // 发送一次Handler
        mHandler.sendEmptyMessage(NEXT_HANDLER_MSG);

        Fragment fragment2 = supportFragmentManager.findFragmentByTag(FRAGMENT_ACTIVITY_NAME);
        Log.d(TAG, "RequestManager: fragment2" + fragment2); // null : @3 还在排队中,还没有消费
    }

    /**
     * 可以管理生命周期 -- Activity是有生命周期方法的(Fragment)
     * @param activity
     */
    // todo @2
    public RequestManager(Activity activity) {
        this.requestManagerContext = activity;

        // TODO @3
        // 拿到Fragment
        android.app.FragmentManager fragmentManager = activity.getFragmentManager();
        android.app.Fragment fragment = fragmentManager.findFragmentByTag(ACTIVITY_NAME);
        if (null == fragment) {
            fragment = new ActivityFragmentManager(requestTargetEngine); // Fragment的生命周期与requestTargetEngine关联起来了
            // 添加到管理器 -- fragmentManager.beginTransaction().add.. Handler
            fragmentManager.beginTransaction().add(fragment, ACTIVITY_NAME).commitAllowingStateLoss(); // 提交
        }

        android.app.Fragment fragment2 = fragmentManager.findFragmentByTag(ACTIVITY_NAME);
        Log.d(TAG, "RequestManager: fragment2" + fragment2); // null : @3 还在排队中,还没有消费

        // 发送一次Handler
        mHandler.sendEmptyMessage(NEXT_HANDLER_MSG);

    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Fragment fragment2 = fragmentActivity.getSupportFragmentManager().findFragmentByTag(FRAGMENT_ACTIVITY_NAME);
            Log.d(TAG, "Handler: fragment2" + fragment2); // 有值 : 不在排队中,所以有值
            return false;
        }
    });

    /**
     * 代表无法去管理生命周期 -- 因为Application无法管理
     * @param context
     */
    public RequestManager(Context context) {
        this.requestManagerContext = context;
    }

    /**
     * load 拿到要显示的图片路径
     * @param s
     * @return
     */
    public RequestTargetEngine load(String s) {
        // 移除Handler
        mHandler.removeMessages(NEXT_HANDLER_MSG);

        return requestTargetEngine;
    }
}

fragmentManager.beginTransaction().add
之后立马get是拿不到fragment的,因为此时的add任务是发送的handler的队列中的,只有从队列中取出执行了,才会get到fragment。所以通过Handler发送消息的方式拿刚add进去的fragment

RequestManagerRetriver

/**
 * 管理RequestManager
 */
public class RequestManagerRetriver {

    public RequestManager get(FragmentActivity fragmentActivity) {
        return new RequestManager(fragmentActivity);
    }

    public RequestManager get(Activity activity) {
        return new RequestManager(activity);
    }

    public RequestManager get(Context context) {
        return new RequestManager(context);
    }
}

ActivityFragmentManager

是与Activity生命周期绑定的辅助类,通过回调接口LifecycleCallback 将生命周期的回调方法暴露给外部。

/**
 * Activity 生命周期关联管理
 */
public class ActivityFragmentManager extends Fragment {

    public ActivityFragmentManager(){}

    private LifecycleCallback lifecycleCallback;

    @SuppressLint("ValidFragment")
    public ActivityFragmentManager(LifecycleCallback lifecycleCallback) {
        this.lifecycleCallback = lifecycleCallback;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (lifecycleCallback != null) {
            lifecycleCallback.glideInitAction();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (lifecycleCallback != null) {
            lifecycleCallback.glideStopAction();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (lifecycleCallback != null) {
            lifecycleCallback.glideRecycleAction();
        }
    }
}

FragmentActivityFragmentManager

是与FragmentActivity生命周期绑定的辅助类,通过回调接口LifecycleCallback 将生命周期的回调方法暴露给外部。

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;

/**
 * FragmentActivity 生命周期关联管理
 */
public class FragmentActivityFragmentManager extends Fragment {

    public FragmentActivityFragmentManager(){}

    private LifecycleCallback lifecycleCallback;

    @SuppressLint("ValidFragment")
    public FragmentActivityFragmentManager(LifecycleCallback lifecycleCallback) {
        this.lifecycleCallback = lifecycleCallback;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (lifecycleCallback != null) {
            lifecycleCallback.glideInitAction();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (lifecycleCallback != null) {
            lifecycleCallback.glideStopAction();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (lifecycleCallback != null) {
            lifecycleCallback.glideRecycleAction();
        }
    }

}

LifecycleCallback

生命周期的回调接口

public interface LifecycleCallback {

    // 生命周期初始化了
    public void glideInitAction();

    // 生命周期 停止了
    public void glideStopAction();

    // 生命周期 释放 操作了
    public void glideRecycleAction();

}

RequestTargetEngine

图片加载的核心类, 实现了生命周期接口,从而与Activity或者FragmentActivity的生命周期绑定在一起。

/**
 * 加载图片资源
 */
public class RequestTargetEngine implements LifecycleCallback {

    private final String TAG = RequestTargetEngine.class.getSimpleName();

    @Override
    public void glideInitAction() {
        Log.d(TAG, "glideInitAction: Glide生命周期之 已经开启了 初始化了....");
    }

    @Override
    public void glideStopAction() {
        Log.d(TAG, "glideInitAction: Glide生命周期之 已经停止中 ....");
    }

    @Override
    public void glideRecycleAction() {
        Log.d(TAG, "glideInitAction: Glide生命周期之 进行释放操作 缓存策略释放操作等 >>>>>> ....");
    }

    public void into(ImageView imageView) {
    }
}

你可能感兴趣的:(android,Android,Glide)