RxLifecycle
1.1、RxLifecycle简介

RxJava使用不好的话,很容易导致内存泄露。当发布了一个订阅后,由于没有及时取消,导致ActivityFragment无法销毁导致的内存泄露。

RxLifecycle是通过监听ActivityFragment的生命周期,来自动断开订阅,以防止内存泄漏。

RxLifecycleGitHub上的网址:https://github.com/trello/RxLifecycle

1.2、在build.gradle里面添加依赖

RxLifecycle v1的依赖:

compile 'com.trello:rxlifecycle:1.0'

// If you want to bind to Android-specific lifecycles
compile 'com.trello:rxlifecycle-android:1.0'

// If you want pre-written Activities and Fragments you can subclass as providers
compile 'com.trello:rxlifecycle-components:1.0'

// If you want to use Navi for providers
compile 'com.trello:rxlifecycle-navi:1.0'

// If you want to use Kotlin syntax
compile 'com.trello:rxlifecycle-kotlin:1.0'

RxLifecycle v2的依赖:

compile 'com.trello.rxlifecycle2:rxlifecycle:2.0.1'

// If you want to bind to Android-specific lifecycles
compile 'com.trello.rxlifecycle2:rxlifecycle-android:2.0.1'

// If you want pre-written Activities and Fragments you can subclass as providers
compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.0.1'

// If you want to use Navi for providers
compile 'com.trello.rxlifecycle2:rxlifecycle-navi:2.0.1'

// If you want to use Kotlin syntax
compile 'com.trello.rxlifecycle2:rxlifecycle-kotlin:2.0.1'

这个依赖可能会导致如下错误:

Warning:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (3.0.1) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

解决的办法就是在build.gradle中设置如下:

android {
    ...
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
}
1.3、RxAppCompatActivity

RxAppCompatActivity的源码如下:

package com.trello.rxlifecycle2.components.support;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.RxLifecycle;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.RxLifecycleAndroid;
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

public abstract class RxAppCompatActivity extends AppCompatActivity implements LifecycleProvider {

    private final BehaviorSubject lifecycleSubject = BehaviorSubject.create();

    @Override
    @NonNull
    @CheckResult
    public final Observable lifecycle() {
        return lifecycleSubject.hide();
    }

    @Override
    @NonNull
    @CheckResult
    public final  LifecycleTransformer bindUntilEvent(@NonNull ActivityEvent event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    @Override
    @NonNull
    @CheckResult
    public final  LifecycleTransformer bindToLifecycle() {
        return RxLifecycleAndroid.bindActivity(lifecycleSubject);
    }

    @Override
    @CallSuper
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lifecycleSubject.onNext(ActivityEvent.CREATE);
    }

    @Override
    @CallSuper
    protected void onStart() {
        super.onStart();
        lifecycleSubject.onNext(ActivityEvent.START);
    }

    @Override
    @CallSuper
    protected void onResume() {
        super.onResume();
        lifecycleSubject.onNext(ActivityEvent.RESUME);
    }

    @Override
    @CallSuper
    protected void onPause() {
        lifecycleSubject.onNext(ActivityEvent.PAUSE);
        super.onPause();
    }

    @Override
    @CallSuper
    protected void onStop() {
        lifecycleSubject.onNext(ActivityEvent.STOP);
        super.onStop();
    }

    @Override
    @CallSuper
    protected void onDestroy() {
        lifecycleSubject.onNext(ActivityEvent.DESTROY);
        super.onDestroy();
    }
}

你可以发现,实际上,他是调用RxLifecycle实现的。

使用此类必须通过继承实现。

1.3.1、public <T> LifecycleTransformer<T> bindToLifecycle()

示例:

RxView.clicks(findViewById(R.id.fab)).compose(this.bindToLifecycle()).subscribe(observer);
1.3.2、public <T> LifecycleTransformer<T> bindUntilEvent(ActivityEvent event)

指定解除绑定的Activity生命周期。

示例:

RxView.clicks(findViewById(R.id.fab)).compose(this.bindUntilEvent(ActivityEvent.DESTROY)).subscribe(observer);

这可以实现,当Activity退出后取消网络请求等。

1.4、RxFragment、RxDialogFragment、RxPreferenceFragment

这几个类与RxAppCompatActivity的使用方法完全一样。

1.5、ActivityEvent

ActivityEvent的源码如下:

public enum ActivityEvent {
    CREATE,
    START,
    RESUME,
    PAUSE,
    STOP,
    DESTROY
}

显然,就是Activity生命周期的枚举。

1.6、FragmentEvent

FragmentEvent的源码如下:

public enum FragmentEvent {
    ATTACH,
    CREATE,
    CREATE_VIEW,
    START,
    RESUME,
    PAUSE,
    STOP,
    DESTROY_VIEW,
    DESTROY,
    DETACH
}

显然,就是Fragment生命周期的枚举。

1.7、RxLifecycle

如果你不想通过继承RxAppCompatActivityRxFragmentRxDialogFragmentRxPreferenceFragment这些类来管理生命周期,我们可以通过调用RxLifecycle来实现,从这些类的实现看,他们都是通过调用RxLifecycle的方法实现的。

1.7.1、public static <T, R> LifecycleTransformer<T> bind(Observable<R> lifecycle)

示例:

package com.fpliu.xx;

import com.jakewharton.rxbinding2.view.RxView;
import com.trello.rxlifecycle2.RxLifecycle;

import io.reactivex.subjects.BehaviorSubject;

public final class MainActivity extends AppCompatActivity {

    private final BehaviorSubject lifecycleSubject = BehaviorSubject.create();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        RxView.clicks(findViewById(R.id.fab)).compose(RxLifecycle.bind(lifecycleSubject)).subscribe();
    }

    @Override
    protected void onDestroy() {
        lifecycleSubject.onNext(ActivityEvent.DESTROY);
        super.onDestroy();
    }
}
1.7.2、public static <T, R> LifecycleTransformer<T> bindUntilEvent(Observable<R> lifecycle, R event)

指定解除绑定的Activity生命周期。

示例:

package com.fpliu.xx;

import com.jakewharton.rxbinding2.view.RxView;
import com.trello.rxlifecycle2.RxLifecycle;
import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.subjects.BehaviorSubject;

public final class MainActivity extends AppCompatActivity {

    private final BehaviorSubject lifecycleSubject = BehaviorSubject.create();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        RxView.clicks(findViewById(R.id.fab)).compose(RxLifecycle.bindUntilEvent(lifecycleSubject, ActivityEvent.DESTROY)).subscribe();
    }

    @Override
    protected void onDestroy() {
        lifecycleSubject.onNext(ActivityEvent.DESTROY);
        super.onDestroy();
    }
}

这可以实现,当Activity退出后取消网络请求等。