RxJava/RxAndroid:ReplaySubject
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.subjects.ReplaySubject;
public class MainActivity extends AppCompatActivity {
private String TAG = "输出";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReplaySubject mReplaySubject = ReplaySubject.create(128);
mReplaySubject.subscribe(mDisposableObserver1);
mReplaySubject.onNext(0);
mReplaySubject.onNext(1);
mReplaySubject.onNext(2);
mReplaySubject.subscribe(mDisposableObserver2);
}
private DisposableObserver mDisposableObserver1 = new DisposableObserver<Integer>() {
@Override
public void onNext(Integer i) {
Log.d(TAG, "onNext-1:" + i);
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete-1");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
private DisposableObserver mDisposableObserver2 = new DisposableObserver<Integer>() {
@Override
public void onNext(Integer integer) {
Log.d(TAG, "onNext-2:" + integer);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete-2");
}
};
}
output:
01-23 17:16:15.802 5165-5165/zhangphil.book D/输出: onNext-1:0
01-23 17:16:15.802 5165-5165/zhangphil.book D/输出: onNext-1:1
01-23 17:16:15.802 5165-5165/zhangphil.book D/输出: onNext-1:2
01-23 17:16:15.802 5165-5165/zhangphil.book D/输出: onNext-2:0
01-23 17:16:15.802 5165-5165/zhangphil.book D/输出: onNext-2:1
01-23 17:16:15.802 5165-5165/zhangphil.book D/输出: onNext-2:2