我正在使用 rxjava 2 并尝试使用 rxbus 传递值
rxbus 代码
public class SeasonTabSelectorBus {
private static SeasonTabSelectorBus instance;
private PublishSubject<Object> subject = PublishSubject.create();
public static SeasonTabSelectorBus instanceOf() {
if (instance == null) {
instance = new SeasonTabSelectorBus();
}
return instance;
}
public void setTab(Object object) {
try {
subject.onNext(object);
subject.onComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
public Observable<Object> getSelectedTab() {
return subject;
}
}
我将值设置为
SeasonTabSelectorBus.instanceOf().setTab(20);
这是我的订阅代码
SeasonTabSelectorBus.instanceOf().getSelectedTab().subscribe(new Observer<Object>(){
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Object o) {
if (o instanceof Integer) {
int seasonSelected =(int) o;
Log.e("season selected",seasonSelected+"");
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
现在我在第一次调用时得到了值,但是当我用不同的值再次调用时,我没有得到回调。
SeasonTabSelectorBus.instanceOf().setTab(40);
SeasonTabSelectorBus.instanceOf().setTab(90);
SeasonTabSelectorBus.instanceOf().setTab(120);
SeasonTabSelectorBus.instanceOf().setTab(290);