I need to pass some data between two actions MainActivityand ChildActivity. Click the button MainActivityto open ChildActivityand send an event with data. I have a singleton:
Subject<Object, Object> subject = new SerializedSubject<>(PublishSubject.create());
and in MainActivityI have the following button click handler:
public void onClick(){
startActivity(new Intent(MainActivity.this, ChildActivity.class));
subject.onNext(new SomeEvent(data));
}
and event listener subscriber in ChildActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addEventListeners();
}
private void addEventListeners() {
subject.ofType(SomeEvent.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io()).subscribe(
event -> {
loadData(event.getData());
});
}
When I dispatch an event after the activity is triggered , and the call addEventListenersto ChildActivity onCreateis still not subscribed to this event, loadData()not called.
What is the correct way to transfer data between actions using RxJava (if possible)?