Android 第三方庫RxLifecycle使用

來源:互聯網
上載者:User

標籤:state   second   web   實現   can   綁定   ado   使用方法   關係   

1.簡單介紹RxLifecycle

1.1.使用原因。

  在使用rxjava的時候,如果沒有及時解除訂閱,在退出activity的時候,非同步線程還在執行。

  對activity還存在引用,此時就會產生記憶體流失。

  RxLifecycle就是為瞭解決rxjava導致的記憶體流失而產生的。

 

 

1.2.RxLifecycle可以做到什麼呢?  

  它可以讓Observable發布的事件和當前的組件綁定,實現生命週期同步。

  從而實現當前組件生命週期結束時,自動取消對Observable訂閱。

  核心思想:通過監聽Activity、Fragment的生命週期,來自動斷開訂閱防止記憶體流失。

 

1.3.參考文獻。

  RxAndroid的github地址:https://github.com/ReactiveX/RxAndroid

  Rxlifecycle的github地址:https://github.com/trello/RxLifecycle

  參考文章:Rxlifecycle使用詳解。

  參考文章:在mvp中使用rxlifecycle避免rxjava的記憶體流失。  

 


2.使用方法

2.1.添加最新的依賴 

//rxjava的依賴compile ‘io.reactivex.rxjava2:rxandroid:2.0.1‘compile ‘io.reactivex.rxjava2:rxjava:2.1.7‘//rxlifecycle的依賴compile ‘com.trello.rxlifecycle2:rxlifecycle-android-lifecycle-kotlin:2.2.1‘

 

 

2.2.Activity以及Fragment的繼承

  Activity需要繼承RxAppCompatActivity。==>比如這麼一個類

public class BaseActivity extends RxAppCompatActivity

  Fragment需要繼承RxFragment。==>比如這麼一個類

public abstract class BaseFragment<T extends IBasePresenter>        extends RxFragment        implements IBaseView<T>

 

 

2.3.綁定生命週期。

  自己寫一個介面,裡面定義一個bindToLife()函數。  

 /**     * 綁定生命週期     */    <T> LifecycleTransformer<T> bindToLife();

  這個bindToLife()是自訂的名字,這裡就叫做bindToLife好了。

  這個介面是自己定義的一個介面,我讓Fragment來繼承這個介面。

  這個返回的是一個LifecycleTransformer<T>,這個官方的一個類。而且這裡使用了泛型,更加通用了。

 

 

2.4.在自己的Fragment中實現這個介面函數。 

/**     * 綁定生命週期     */    @Override    public <T> LifecycleTransformer<T> bindToLife() {        return bindUntilEvent(FragmentEvent.DESTROY);    }

  這裡調用了官方的一個bindUntilEvent的方法,以此函數來綁定生命週期。

  這裡的FragmentEvent.DESTROY,顧名思義,就是當Fragment銷毀的時候,自動解除訂閱關係。

  

  注意:這裡使用了FragmentEvent類,其中的CREATE/START/RESUME/PAUSE/STOP/DESTROY  

        分布對應生命週期內的方法。使用bindUntilEvent指定在哪個生命週期方法調用示取消訂閱。

 

 

2.5.在請求API函數的時候,執行自己寫的bindToLife()

    調用Observable<T>.compose(執行介面中的bindToLife())

  compose裡面的東西應該是:LifecycleTransformer<T>。 

 


3.對於Rxlifecycle的理解

3.1.使用原因就不用多說了,解決RxJava導致的記憶體流失。

 

 

3.2.因為activity和fragment控制切換線程的時候,引用沒有清除乾淨,所以就把手腳著重於activity和fragment的

  生命週期了,就是說,如果你的活動如果碰到意外或者使用者主動銷毀,那麼也斷開訂閱,就是訂閱的生命週期

  和活動的生命週期已經融為一體了,那就不用擔心再發生記憶體流失了,所以這種綁定生命週期的方法是從根本

  上解決了記憶體流失。感覺沒有更好的方法解決了。

 

 

3.3.其實官方為了兩種解決方案。

  一種是手動取消訂閱,我不知道怎麼用,也沒有實戰過。

  這裡提供一下大神的文章:RxAndroid之Rxlifecycle使用。

  手動設定在activity onPause的時候取消訂閱。

private static final String TAG = "RxLifecycle";@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    Log.d(TAG, "onCreate()");    setContentView(R.layout.activity_main);    Observable.interval(1, TimeUnit.SECONDS)            .doOnUnsubscribe(new Action0() {                @Override                public void call() {                    Log.i(TAG, "Unsubscribing subscription from onCreate()");                }            })            //Note:手動設定在activity onPause的時候取消訂閱            .compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE))            .subscribe(new Action1<Long>() {                @Override                public void call(Long num) {                    Log.i(TAG, "Started in onCreate(), running until onPause(): " + num);                }            });}

 

  手動設定在activity的onDestroy的時候取消訂閱。

@Overrideprotected void onResume() {    super.onResume();    Log.d(TAG, "onResume()");    // `this.<Long>` is necessary if you‘re compiling on JDK7 or below.    // If you‘re using JDK8+, then you can safely remove it.    Observable.interval(1, TimeUnit.SECONDS)            .doOnUnsubscribe(new Action0() {                @Override                public void call() {                    Log.i(TAG, "Unsubscribing subscription from onResume()");                }            })            //Note:手動設定在activity onDestroy的時候取消訂閱            .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))            .subscribe(new Action1<Long>() {                @Override                public void call(Long num) {                    Log.i(TAG, "Started in onResume(), running until in onDestroy(): " + num);                }            });}

 

  自動化佈建取消訂閱,預設在onStart的時候調用,在onStop的時候取消訂閱。

@Overrideprotected void onStart() {    super.onStart();    Log.d(TAG, "onStart()");// Using automatic unsubscription, this should determine that the correct time to    // unsubscribe is onStop (the opposite of onStart).    Observable.interval(1, TimeUnit.SECONDS)            .doOnUnsubscribe(new Action0() {                @Override                public void call() {                    Log.i(TAG, "Unsubscribing subscription from onStart()");                }            })            //Note:bindToLifecycle的自動取消訂閱樣本,因為是在onStart的時候調用,所以在onStop的時候自動取消訂閱            .compose(this.<Long>bindToLifecycle())            .subscribe(new Action1<Long>() {                @Override                public void call(Long num) {                    Log.i(TAG, "Started in onStart(), running until in onStop(): " + num);                }            });}

 



Android 第三方庫RxLifecycle使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.