探究爲何Glide 能夠與 Activity 生命週期綁定

用過Glide同窗都知道,Glide的最大不一樣之處就是能夠與Activity 生命週期綁定,在Activity onDestroy時自動結束網絡請求,釋放資源。 那麼爲何Glide 能夠作到與Activity綁定生命週期,而且是怎麼作到到呢? 咱們經過一些代碼來講明(代碼均來自Glide源碼):android

首先咱們建立一個 LifeCycleListener

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();
}

複製代碼

建立 LifeCycleListener 管理類

public class ActivityFragmentLifeCycle {
    private LifeCycleListener lifecycleListener;

    public void addListener(@NonNull LifeCycleListener listener) {
        lifecycleListener = listener;
    }

    void onStart() {
        if (lifecycleListener != null)
            lifecycleListener.onStart();

    }

    void onStop() {
        if (lifecycleListener != null)
            lifecycleListener.onStop();

    }

    void onDestroy() {
        if (lifecycleListener != null)
            lifecycleListener.onDestroy();
    }
}

複製代碼

建立一個操做ActivityFragmentLifeCycleFragment

public class SupportManagerFragment extends Fragment {
    private ActivityFragmentLifeCycle lifecycle;
    public SupportManagerFragment() {
        this(new ActivityFragmentLifeCycle());
    }

    @SuppressLint("ValidFragment")
    public SupportManagerFragment(@NonNull ActivityFragmentLifeCycle lifeCycle) {
        this.lifecycle = lifeCycle;
    }

    @NonNull
    ActivityFragmentLifeCycle getLifeCycle() {
        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();
    }
}

複製代碼

這裏就是關鍵類了,爲何要有個Fragment?由於他綁定Activity生命週期的方式就是給當前Activity加上Fragment經過這個Fragment 來管理生命週期。 下面就介紹如何建立Fragment依託於當前的Activity得到生命週期管理git

得到Activity ,來建立Fragment管理生命週期

/**
 * Create by JokAr. on 2018/12/24.
 */
public class RequestManager implements LifeCycleListener {

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

    /**
     * 綁定activity
     * @param context 傳入的上下文
     */
    public void bind(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) {
                bind((FragmentActivity) context);
            } else if (context instanceof Activity) {
                bind((AppCompatActivity) context);
            } else if (context instanceof ContextWrapper) {
                bind(((ContextWrapper) context).getBaseContext());
            }
        }
    }

    
    private void bind(AppCompatActivity activity) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        supportFragmentGet(fragmentManager, isActivityVisible(activity));
    }

    private void bind(FragmentActivity activity) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        supportFragmentGet(fragmentManager, isActivityVisible(activity));
    }

    /**
     * 建立Fragment 並依託於當前Activity(固然Glide作了緩存去重操做) 
     * @param fragmentManager
     * @param activityVisible
     */
    private void supportFragmentGet(FragmentManager fragmentManager, boolean activityVisible) {
        SupportManagerFragment current = new SupportManagerFragment();

        fragmentManager.beginTransaction()
                .add(current, "managerLife")
                .commitAllowingStateLoss();
        current.getLifeCycle().addListener(this);
        if (activityVisible) {
            current.getLifeCycle().onStart();
        }
    }

    private boolean isActivityVisible(Activity activity) {
        return !activity.isFinishing();
    }

    @Override
    public void onStart() {
        //do you buzz
        Log.i(TAG, "onStart: ");
    }

    /**
     * 當出現該函數時,能夠暫停Glide網絡圖片的請求
     */
    @Override
    public void onStop() {
        //do you buzz
        Log.i(TAG, "onStop: ");
    }

    @Override
    public void onDestroy() {
        //do you buzz
        Log.i(TAG, "onDestroy: ");
    }
}

複製代碼

調用

RequestManager requestManager = new RequestManager();
requestManager.bind(this);
複製代碼

經過這樣,咱們就能夠本身實現綁定Activity生命週期管理,也知道了Glide 是怎樣綁定的github

相關文章
相關標籤/搜索