RxJava
使用不好的话,很容易导致内存泄露。当发布了一个订阅后,由于没有及时取消,导致Activity
或Fragment
无法销毁导致的内存泄露。
RxLifecycle
是通过监听Activity
或Fragment
的生命周期,来自动断开订阅,以防止内存泄漏。
RxLifecycle
在GitHub上的网址:https://github.com/trello/RxLifecycle
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'
}
}
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
实现的。
使用此类必须通过继承实现。
示例:
RxView.clicks(findViewById(R.id.fab)).compose(this.bindToLifecycle()).subscribe(observer);
指定解除绑定的Activity
生命周期。
示例:
RxView.clicks(findViewById(R.id.fab)).compose(this.bindUntilEvent(ActivityEvent.DESTROY)).subscribe(observer);
这可以实现,当Activity
退出后取消网络请求等。
这几个类与RxAppCompatActivity
的使用方法完全一样。
ActivityEvent
的源码如下:
public enum ActivityEvent {
CREATE,
START,
RESUME,
PAUSE,
STOP,
DESTROY
}
显然,就是Activity
生命周期的枚举。
FragmentEvent
的源码如下:
public enum FragmentEvent {
ATTACH,
CREATE,
CREATE_VIEW,
START,
RESUME,
PAUSE,
STOP,
DESTROY_VIEW,
DESTROY,
DETACH
}
显然,就是Fragment
生命周期的枚举。
如果你不想通过继承RxAppCompatActivity
、RxFragment
、RxDialogFragment
、RxPreferenceFragment
这些类来管理生命周期,我们可以通过调用RxLifecycle
来实现,从这些类的实现看,他们都是通过调用RxLifecycle
的方法实现的。
示例:
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();
}
}
指定解除绑定的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
退出后取消网络请求等。