How pass data between activities using RxJava in android?
Asked Answered
C

2

7

I need to pass some data between two activities MainActivity and ChildActivity. Button click on MainActivity should open ChildActivity and send event with data. I have singleton:

Subject<Object, Object> subject = new SerializedSubject<>(PublishSubject.create());

and in MainActivity I have the following button click handler:

    public void onClick(){
        startActivity(new Intent(MainActivity.this, ChildActivity.class));
        subject.onNext(new SomeEvent(data));
    }

and event listener subscription 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 send event after starting activity and call addEventListeners in ChildActivity onCreate is still not subscribed to this event and loadData() is not called.

What is proper way to pass data between activities using RxJava (if it's possible)?

Catlike answered 21/9, 2016 at 14:59 Comment(4)
Probably you can use Behavioral Subject instead of Publish Subject which keeps the last few events and emita them immidately on new subscription.Salk
@SagarTrehan yes, it solves my problem. Could you add answer to this question instead of comment?Catlike
Glad it helps you fixing the issue. I have posted my answerSalk
Any problems with Intents ?Sportscast
S
5

Reason:

Problem is that you are using PublishSubject. As per documentation of PublishSubject emits all the subsequent items of the source Observable at the time of the subscription. So in your case it will emit event only if it is subscribed.

Fix for your problem

Instead of using PublishSubject use BehaviorSubject which emits the most recently emitted item and all the subsequent items of the source Observable when a observer subscribe to it.

Browse following link for more details.

Salk answered 22/9, 2016 at 14:4 Comment(0)
T
12

if anybody needs a complete solution to send data between activities using RxJava2

1- Create the bus:

public final class RxBus {

private static final BehaviorSubject<Object> behaviorSubject
        = BehaviorSubject.create();


public static BehaviorSubject<Object> getSubject() {
    return behaviorSubject;
}

}

2- the sender activity

                       //the data to be based
                    MyData  data =getMyData();
                    RxBus.getSubject().onNext(data) ;
                    startActivity(new Intent(MainActivity.this, AnotherAct.class));

3-the receiver activity

    disposable = RxBus.getSubject().
            subscribeWith(new DisposableObserver<Object>() {


                @Override
                public void onNext(Object o) {
                    if (o instanceof MyData) {
                   Log.d("tag", (MyData)o.getData();
                    }
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
    });

4-unSubscribe to avoid memory leacks:

 @Override
protected void onDestroy() {
    super.onDestroy();
    disposable.dispose();
}
Teacher answered 13/12, 2017 at 7:29 Comment(2)
@Bishboy Abd do you need to call subscription.unsubscribe() method?Cotangent
When receiver activity starts again onNext is calling again, i called dispose() method onDestroy().Cotangent
S
5

Reason:

Problem is that you are using PublishSubject. As per documentation of PublishSubject emits all the subsequent items of the source Observable at the time of the subscription. So in your case it will emit event only if it is subscribed.

Fix for your problem

Instead of using PublishSubject use BehaviorSubject which emits the most recently emitted item and all the subsequent items of the source Observable when a observer subscribe to it.

Browse following link for more details.

Salk answered 22/9, 2016 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.