How Glide is related to the important life cycle of Activity

3419 단어

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

좋은 웹페이지 즐겨찾기