管理Activity(Fragment、dialogFragment)的生命周期需要在build.gradle中加入compile 'com.trello:rxlifecycle-components:0.6.1'
在Activity或者Fragment中管理Observable的生命周期是很有必要的,未完成的订阅会引起内存泄漏
你必须提供一个Observable<ActivityEvent>或者是Observable<FragmentEvent>来告诉RxLifecycle你需要停止序列发射的时间
你可以通过下面的方式显式停止序列:
myObservable
.compose(RxLifecycle.bindUntilEvent(lifecycle, ActivityEvent.DESTROY))
.subscribe();
你也可以让RxLifecycle决定在何时的时间停止序列的发送
myObservable
.compose(RxLifecycle.bindActivity(lifecycle))
.subscribe();
//发射的内容,每1s发射一条数据
Observable.interval(1, TimeUnit.SECONDS)
//序列停止发射后执行的方法
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
Log.e(TAG, "Unsubscribing subscription from onCreate()");
}
})
//指定在哪个生命周期<span style="font-family: Arial, Helvetica, sans-serif;">序列停止发射</span>
.compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE))
//订阅subscriber
.subscribe(new Action1<Long>() {
@Override
public void call(Long num) {
Log.e(TAG, "Started in onCreate(), running until onPause(): " + num);
}
});
1.指定在某个声明周期结束
compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE))
2.使用RxActivity或者RxFragment默认的声明周期管理来管理
compose(this.<Long>bindToLifecycle())
注:RxActivity、RxFragment中默认的取消订阅的时间
//RxActivity中的默认LifeCycle
// Figures out which corresponding next lifecycle event in which to unsubscribe, for Activities
private static final Func1<ActivityEvent, ActivityEvent> ACTIVITY_LIFECYCLE =
new Func1<ActivityEvent, ActivityEvent>() {
@Override
public ActivityEvent call(ActivityEvent lastEvent) {
switch (lastEvent) {
case CREATE: //如果在onCreate中订阅,则在onDestroy中序列被停止发射
return ActivityEvent.DESTROY;
case START: //如果在onStart中订阅,则在onStop中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return ActivityEvent.STOP;
case RESUME: //如果在onResume中订阅,则在onPause中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return ActivityEvent.PAUSE;
case PAUSE: //如果在onPause中订阅,则在onStop中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return ActivityEvent.STOP;
case STOP: //如果在onStop中订阅,则在onDestroy中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return ActivityEvent.DESTROY;
case DESTROY: //如果在onDestroy中订阅,则抛出异常
throw new OutsideLifecycleException("Cannot bind to Activity lifecycle when outside of it.");
default:
throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented");
}
}
};
//RxFragment中的默认LifeCycle
// Figures out which corresponding next lifecycle event in which to unsubscribe, for Fragments
private static final Func1<FragmentEvent, FragmentEvent> FRAGMENT_LIFECYCLE =
new Func1<FragmentEvent, FragmentEvent>() {
@Override
public FragmentEvent call(FragmentEvent lastEvent) {
switch (lastEvent) {
case ATTACH: //如果在onAttach中订阅,则在onDetach中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.DETACH;
case CREATE: //如果在onCreate中订阅,则在onDestroy中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.DESTROY;
case CREATE_VIEW: //如果在onCreateView中订阅,则在onDestroyView中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.DESTROY_VIEW;
case START: //如果在onStart中订阅,则在onStop中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.STOP;
case RESUME: //如果在onResume中订阅,则在onPause中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.PAUSE;
case PAUSE: //如果在onPause中订阅,则在onStop中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.STOP;
case STOP: //如果在onStop中订阅,则在onDestroy中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.DESTROY_VIEW;
case DESTROY_VIEW: //如果在onDestroyView中订阅,则在onDestroyView中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.DESTROY;
case DESTROY: //如果在onDestroyView中订阅,则在onDestroyView中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
return FragmentEvent.DETACH;
case DETACH: //如果在onDetach中订阅,则抛出异常
throw new OutsideLifecycleException("Cannot bind to Fragment lifecycle when outside of it.");
default:
throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented");
}
}
};
测试代码:
public class RxLifecycleActivity extends RxAppCompatActivity{
private static final String TAG = "RxLifecycle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "onCreate()");
// Specifically bind this until onPause()
//发射的内容,每1s发射一条数据
Observable.interval(1, TimeUnit.SECONDS)
//<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span><span style="font-family: Arial, Helvetica, sans-serif;">后执行的方法</span>
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
Log.e(TAG, "Unsubscribing subscription from onCreate()");
}
})
//指定在哪个生命周期,在onPause()中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
.compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE))
//订阅subscriber
.subscribe(new Action1<Long>() {
@Override
public void call(Long num) {
Log.e(TAG, "Started in onCreate(), running until onPause(): " + num);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.e(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.e(TAG, "Unsubscribing subscription from onStart()");
}
})
//使用默认的声明周期取消订阅,在RxActivity中,如果在onStart中订阅,则在onStop中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
.compose(this.<Long>bindToLifecycle())
.subscribe(new Action1<Long>() {
@Override
public void call(Long num) {
Log.e(TAG, "Started in onStart(), running until in onStop(): " + num);
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.e(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.e(TAG, "Unsubscribing subscription from onResume()");
}
})
//指定在onDestroy()中<span style="font-family: Arial, Helvetica, sans-serif;">序列被停止发射</span>
.compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(new Action1<Long>() {
@Override
public void call(Long num) {
Log.e(TAG, "Started in onResume(), running until in onDestroy(): " + num);
}
});
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy()");
}
运行结果:
注:1.参考资料https://github.com/trello/RxLifecycle
2.RxLifecycle不会自动取消订阅,他只是停止了序列继续发射,取消订阅需要手动执行