摘要:從這段代碼入手分析分析從這段代碼可以看出無論傳入的是還是或者干脆傳入或都會調用這個方法而這個方法生成兩個對象對象,并把它加到上對象這兩個對象擁有共同的對象對象,當系統調用的生命周期,的生命周期隨之被調用來處理列表,將的生命周期與的生命周期聯
從這段代碼入手分析Glide
Glide.with(context) .load(url) .placeholder(R.drawable.loading) .error(R.drawable.avatar) .listener(new RequestListener1.Glide.with(context)分析() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { LogUtil.logV("loadPhotoDebug", "loadAvatar onResourceReady url = " + url); if (isSimpleImage(tagView.getTag(), url)) { return false; } mImageView.setImageResource(R.drawable.loading); return true; } }) .bitmapTransform(new CropCircleTransformation(context)) .into(mImageView);
Glide.java:
public static RequestManager with(Context context) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(context); }
RequestManagerRetriever.java:
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 get((FragmentActivity) context); } else if (context instanceof Activity) { return get((Activity) context); } else if (context instanceof ContextWrapper) { return get(((ContextWrapper) context).getBaseContext()); } } return getApplicationManager(context); } public RequestManager get(FragmentActivity activity) { if (Util.isOnBackgroundThread()) { return get(activity.getApplicationContext()); } else { assertNotDestroyed(activity); FragmentManager fm = activity.getSupportFragmentManager(); return supportFragmentGet(activity, fm); } } public RequestManager get(Fragment fragment) { if (fragment.getActivity() == null) { throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached"); } if (Util.isOnBackgroundThread()) { return get(fragment.getActivity().getApplicationContext()); } else { FragmentManager fm = fragment.getChildFragmentManager(); return supportFragmentGet(fragment.getActivity(), fm); } }
從這段代碼可以看出無論Glide.with傳入的Context是Fragment還是Activity或者干脆傳入Framgent或FragemtnActivity,都會調用supportFragmentGet這個方法
RequestManagerRetriever.java:
RequestManager supportFragmentGet(Context context, FragmentManager fm) { SupportRequestManagerFragment current = getSupportRequestManagerFragment(fm); RequestManager requestManager = current.getRequestManager(); if (requestManager == null) { requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode()); current.setRequestManager(requestManager); } return requestManager; } SupportRequestManagerFragment getSupportRequestManagerFragment(final FragmentManager fm) { SupportRequestManagerFragment current = (SupportRequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG); if (current == null) { current = pendingSupportRequestManagerFragments.get(fm); if (current == null) { current = new SupportRequestManagerFragment(); pendingSupportRequestManagerFragments.put(fm, current); fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss(); handler.obtainMessage(ID_REMOVE_SUPPORT_FRAGMENT_MANAGER, fm).sendToTarget(); } } return current; }
而supportFragmentGet這個方法生成兩個對象:
1.SupportRequestManagerFragment對象, 并把它加到FragmentManager上;
2.RequestManager對象
這兩個對象擁有共同的對象LifeStyle對象,當Android系統調用SupportRequestManagerFragment的生命周期,RequestManger的生命周期隨之被調用來處理Request列表,
將Fragment的生命周期與RequestManger的生命周期聯系起來:
SupportRequestManagerFragment.java:
@Override public void onStart() { super.onStart(); lifecycle.onStart(); } @Override public void onStop() { super.onStop(); lifecycle.onStop(); } @Override public void onDestroy() { super.onDestroy(); lifecycle.onDestroy(); }
RequestManager.java
public class RequestManager implements LifecycleListener { RequestManager(Context context, final Lifecycle lifecycle, RequestManagerTreeNode treeNode, RequestTracker requestTracker, ConnectivityMonitorFactory factory) { ... ... this.lifecycle = lifecycle; ConnectivityMonitor connectivityMonitor = factory.build(context, new RequestManagerConnectivityListener(requestTracker)); ... ... if (Util.isOnBackgroundThread()) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { lifecycle.addListener(RequestManager.this); } }); } else { lifecycle.addListener(this); } lifecycle.addListener(connectivityMonitor); } @Override public void onStart() { // onStart might not be called because this object may be created after the fragment/activity"s onStart method. resumeRequests(); } /** * Lifecycle callback that unregisters for connectivity events (if the android.permission.ACCESS_NETWORK_STATE * permission is present) and pauses in progress loads. */ @Override public void onStop() { pauseRequests(); } /** * Lifecycle callback that cancels all in progress requests and clears and recycles resources for all completed * requests. */ @Override public void onDestroy() { requestTracker.clearRequests(); }
如果傳入的是Applicaton Context,
private RequestManager getApplicationManager(Context context) { // Either an application context or we"re on a background thread. if (applicationManager == null) { synchronized (this) { if (applicationManager == null) { // Normally pause/resume is taken care of by the fragment we add to the fragment or activity. // However, in this case since the manager attached to the application will not receive lifecycle // events, we must force the manager to start resumed using ApplicationLifecycle. applicationManager = new RequestManager(context.getApplicationContext(), new ApplicationLifecycle(), new EmptyRequestManagerTreeNode()); } } } return applicationManager; }
會得到一個單例的RequestManager對象,RequestManager綁定的生命周期并不會調用onStop,onDestroy,
ApplicationLifecycle.java:
class ApplicationLifecycle implements Lifecycle { @Override public void addListener(LifecycleListener listener) { listener.onStart(); } }
總結:Glide.with(Context)將Activity的生命周期與RequestManager針對的request的處理聯系起來,
傳入ApplicationContext,會得到一個單例的RequestManager對象; 傳入Activity Context,會得到一個屬于一個Activity的RequestManager對象;
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/70708.html
摘要:原文地址游客前言金三銀四,很多同學心里大概都準備著年后找工作或者跳槽。最近有很多同學都在交流群里求大廠面試題。 最近整理了一波面試題,包括安卓JAVA方面的,目前大廠還是以安卓源碼,算法,以及數據結構為主,有一些中小型公司也會問到混合開發的知識,至于我為什么傾向于混合開發,我的一句話就是走上編程之路,將來你要學不僅僅是這些,豐富自己方能與世接軌,做好全棧的裝備。 原文地址:游客kutd...
閱讀 1158·2023-04-26 01:35
閱讀 2513·2021-11-02 14:44
閱讀 7644·2021-09-22 15:38
閱讀 2205·2021-09-06 15:11
閱讀 3720·2019-08-30 15:53
閱讀 795·2019-08-29 16:54
閱讀 631·2019-08-26 13:48
閱讀 1763·2019-08-26 13:47