Greenrobot EventBus event not received
Asked Answered
Q

5

7

I'm using Greenrobot EventBus to pass events from one activity to another.

The flow is something like this: Activity1 starts -> scan a barcode -> Activity2 starts -> accept or deny the response and send an event to Activity1.

So Activity2 sends a new event to Activity1 by doing something like:

@Override
public void onCreate(){
  EventBus.getDefault().register(this);
  // other initialization code
  EventBus.getDefault().post(new MyEvent());
}

In Activity1 I register the event bus and also I have the public onEvent(MyEvent myEvent) method for receiving the event.

The problem is that the onEvent is not triggered. I looked to see maybe there's a problem on the event bus object (like different instances or someting in Activity 1 and 2) but it;s the same instance.

I don't know what seems to be the problem. If somebody could take a look and tell me what am I doing wrong I would much appreciate it.

Thanks!

Quandary answered 9/1, 2015 at 9:36 Comment(2)
Where did u unregister eventbus in your activity?Lauro
Sounds like you just want to use developer.android.com/reference/android/app/… - check out #10407659 if you need help with that.Yseult
M
12

You probably need to use sticky events in this case. After Activity1 starts Activity2 it goes to the background, and can no longer receive any events.

Put this in your Activity1 instead of EventBus.getDefault().register(Object Event)

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().registerSticky(this);
}

and replace

EventBus.getDefault().post(new MyEvent());

in Activity2 with

EventBus.getDefault().postSticky(new MyEvent());

Here is a link to the documentation explaining it

Mut answered 3/3, 2015 at 23:48 Comment(0)
A
3

How does your Activity1 EventBus unregister look like?

I had the same issue because of I was doing this:

Activity1.java

@Override
protected void onStop() {
     super.onStop()
     EventBus.getDefault().unregister(this);
}

The problem with this is that when you start Activity2 onStop gets call, therefore removing the subscription to the event. I was able to solve that by moving the unregister to onDestroy so:

Activity1.java

@Override
protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
}
Alphanumeric answered 27/3, 2017 at 1:5 Comment(0)
H
1

class test

public class TestEventBus {

private String label;

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}
}

Activity A

TestEventBus t = new TestEventBus();
t.setLabel("oi");
EventBus.getDefault().post( t );

Activity B

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onMessageEvent(TestEventBus test) {

Toast.makeText(this, "label "+test.getLabel(), 
Toast.LENGTH_SHORT).show();

};

@Override
public void onStart() {
  super.onStart();
  EventBus.getDefault().register(this);
}

@Override
public void onStop() {
  super.onStop();
  EventBus.getDefault().unregister(this);

 }
Hemiterpene answered 14/3, 2018 at 17:54 Comment(0)
U
0

In new versions to receive sticky events set sticky flag true:

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
fun onNewEvent(event: MessageEvent) {
    //...
}
Urena answered 10/10, 2020 at 22:0 Comment(0)
W
0

That happens because, onStart() you register eventBus then you got to the next activity... so technically event bus is already register in the previous activity... so when you come back to the same activity event bus will crash the app because it already assumed you have registered already....

So Inorder to fix this try to call, on the activity to prevent it from crashing!

@Override
public void onStop() {
  super.onStop();
  EventBus.getDefault().unregister(this);

}

Wessling answered 2/1, 2023 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.