美文网首页
3.Android 手写Glide生命周期让你面试永不忘

3.Android 手写Glide生命周期让你面试永不忘

作者: 鹏城十八少 | 来源:发表于2021-07-05 17:41 被阅读0次

    从月薪3000到年薪60万。从专科生到深圳一线大厂。关注我就能达到大师级水平,这话我终于敢说了, 年薪60万不是梦!

    原理:

    Glide.with(this)绑定了Activity的生命周期。在Activity内新建了一个无UI的Fragment,这个Fragment持有一个Lifecycle,通过Lifecycle在Fragment关键生命周期通知RequestManager进行相关从操作。在生命周期onStart时继续加载,onStop时暂停加载,onDestory时停止加载任务和清除操作。

    面试官:glide调用在子线程会怎么样?

    在UI线程中调用Glide,在子线程使用的话会和使用ApplicationContext一样;

    可以理解为不对请求的生命周期进行管理

    1、传入Application Context或者在子线程使用:调用getApplicationManager(context);这样Glide的生命周期就和应用程序一样了。可以理解为不对请求的生命周期进行管理

    关键的几个类:RequestManager,RequestManagerRetriever ,SupportRequestManagerFragment ,

    Lifecycle ,LifecycleListener

    public class MainActivityextends AppCompatActivity {

    @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            Glide.with(MainActivity.this);

        }

    }

    public class Glide {

    private static Glideglide;

        private Glide() {

    }

    public static synchronized GlidegetInstance() {

    if (glide ==null) {

    glide =new Glide();

            }

    return glide;

        }

    @NonNull

        public static RequestManagerwith(@NonNull FragmentActivity activity) {

    return getRetriever(activity).get(activity);

        }

    @NonNull

        private static RequestManagerRetrievergetRetriever(@Nullable Context context) {

    return new RequestManagerRetriever();

        }

    /***

    * 1.里面是对RequestManager的封装,用fragment生命周期,还是application的生命周期

    * 2.里面绑定fragment和RequestManager

    */

    public class RequestManagerRetriever {

    private volatile RequestManagerapplicationManager;

        @NonNull

        public RequestManagerget(@NonNull Context context) {

    if (context ==null) {

    throw new IllegalArgumentException("You cannot start a load on a null Context");

            }else if (Util.isOnMainThread() && !(contextinstanceof Application)) {

    if (contextinstanceof FragmentActivity) {

    return get((FragmentActivity) context);

                }else if (contextinstanceof Activity) {

    return get((Activity) context);

                }else if (contextinstanceof ContextWrapper

    && ((ContextWrapper) context).getBaseContext().getApplicationContext() !=null) {

    return get(((ContextWrapper) context).getBaseContext());

                }

    }

    return getApplicationManager(context);

        }

    public RequestManagerget(FragmentActivity activity) {

    if (Util.isOnBackgroundThread()) {

    return get(activity.getApplicationContext());

            }else {

    FragmentManager fm = activity.getSupportFragmentManager();

                return supportFragmentGet(activity, fm, null, false);

            }

    }

    /***

        * 绑定逻辑都在这里

        * @param context

        * @param fm

        * @param parentHint

        * @param isParentVisible

        * @return

        */

        @NonNull

        private RequestManagersupportFragmentGet(

    @NonNull Context context,

                @NonNull FragmentManager fm,

                @Nullable Fragment parentHint,

                boolean isParentVisible) {

    SupportRequestManagerFragment current =null;

            RequestManager requestManager = current.getRequestManager();

            if (requestManager ==null) {

    Glide glide = Glide.getInstance();

                requestManager =new RequestManager(glide,current.getGlideLifecycle());

                current.setRequestManager(requestManager);

            }

    return requestManager;

        }

    @NonNull

        private RequestManagergetApplicationManager(@NonNull Context context) {

    if (applicationManager ==null) {

    return applicationManager;

            }

    return applicationManager;

        }

    }

    /***

    * fragment里面设置了requestManager:

    * 里面把ActivityFragmentLifecycle监听回调出去

    */

    public class SupportRequestManagerFragmentextends Fragment {

    private final ActivityFragmentLifecyclelifecycle;

        @Nullable

        private RequestManagerrequestManager;

        public SupportRequestManagerFragment(ActivityFragmentLifecycle lifecycle) {

    this.lifecycle = lifecycle;

        }

    public RequestManagergetRequestManager() {

    return requestManager;

        }

    public void setRequestManager(RequestManager requestManager) {

    this.requestManager = requestManager;

        }

    @NonNull

        ActivityFragmentLifecyclegetGlideLifecycle() {

    return lifecycle;

        }

    @Override

        public void onStart() {

    super.onStart();

            lifecycle.onStart();

        }

    @Override

        public void onStop() {

    super.onStop();

            lifecycle.onStop();

        }

    @Override

        public void onDestroy() {

    super.onDestroy();

            lifecycle.onDestroy();

        }

    }

    public class RequestManagerimplements LifecycleListener {

    Lifecyclelifecycle;

        public RequestManager(Glide glide, ActivityFragmentLifecycle glideLifecycle) {

    lifecycle = glideLifecycle;

            register();

        }

    public RequestManager() {

    }

    public void register() {

    lifecycle.addListener(RequestManager.this);

        }

    @Override

        public void onStart() {

    }

    @Override

        public void onStop() {

    }

    @Override

        public void onDestroy() {

    }

    }

    public interface LifecycleListener {

    /**

        * Callback for when {@link android.app.Fragment#onStart()}} or {@link

        */

        void onStart();

        /**

        * Callback for when {@link android.app.Fragment#onStop()}} or {@link

        */

        void onStop();

        /**

        * Callback for when {@link android.app.Fragment#onDestroy()}} or {@link

        */

        void onDestroy();

    }

    public interface Lifecycle {

    void addListener(@NonNull LifecycleListener listener);

        void removeListener(@NonNull LifecycleListener listener);

    }

    class ActivityFragmentLifecycleimplements Lifecycle {

    private final SetlifecycleListeners =

    Collections.newSetFromMap(new WeakHashMap());

        private boolean isStarted;

        private boolean isDestroyed;

        @Override

        public void addListener(@NonNull LifecycleListener listener) {

    lifecycleListeners.add(listener);

            if (isDestroyed) {

    listener.onDestroy();

            }else if (isStarted) {

    listener.onStart();

            }else {

    listener.onStop();

            }

    }

    @Override

        public void removeListener(@NonNull LifecycleListener listener) {

    lifecycleListeners.remove(listener);

        }

    void onStart() {

    isStarted =true;

            for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {

    lifecycleListener.onStart();

            }

    }

    void onStop() {

    isStarted =false;

            for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {

    lifecycleListener.onStop();

            }

    }

    void onDestroy() {

    isDestroyed =true;

            for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {

    lifecycleListener.onDestroy();

            }

    }

    }

    demo地址:https://github.com/pengcaihua123456/shennandadao

    相关文章

      网友评论

          本文标题:3.Android 手写Glide生命周期让你面试永不忘

          本文链接:https://www.haomeiwen.com/subject/hjwgultx.html