How Glide is related to the important life cycle of Activity
Glide
Glide, its with() method has four or five overloads, which can be passed in Context, Activity, FragmentActivity, Fragment under the v4 package, and Fragment under the app package. Although there are five overloads, there are only two cases in the end, that is, parameters of type Application are passed in, and parameters of non-Application type are passed in.
If you pass in Application, the life cycle of Glide is the same as that of the application. When the application exits, Glide stops loading tasks.
If the incoming non-Application, such as Fragment and Activity, Glide will process the task correspondingly when the important life cycle callback of the corresponding Activity occurs. For example, when Activity.onDestroy() occurs, Glide will respond to this The tasks generated in the Activity are stopped. Avoid wasting resources.
How to do it? In fact, Glide adds a hidden Fragment to the current Activity. Because the Fragment's life cycle is synchronized with the Activity, this Fragment can process the life cycle callback.
Take a look at this Fragment:
public class RequestManagerFragment extends Fragment {
private final ActivityFragmentLifecycle 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();
}
.
.
.
}
The specific listener class is notified using a callback. Take a look at this LifecycleListener:/**
* An interface for listener to {@link android.app.Fragment} and {@link android.app.Activity} lifecycle events.
*/
public interface LifecycleListener {
/**
* Callback for when {@link android.app.Fragment#onStart()}} or {@link android.app.Activity#onStart()} is called.
*/
void onStart();
/**
* Callback for when {@link android.app.Fragment#onStop()}} or {@link android.app.Activity#onStop()}} is called.
*/
void onStop();
/**
* Callback for when {@link android.app.Fragment#onDestroy()}} or {@link android.app.Activity#onDestroy()} is
* called.
*/
void onDestroy();
}
This is the lifecycle monitoring interface defined in Glide. You can see that onStart(), onStop() and onDestroy() are monitored.Glide's RequestManager class implements this interface,
public class RequestManager implements LifecycleListener
Let's take a look at its implementation: @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();
}
It is here that Glide schedules and cancels tasks.Finish
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.