Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple EventBus - No subscribers Registered

I'm trying to implement the absolute basic implementation of EventBus Library for Android.

What I'm trying to simple input content by user in activity 1 and then instead of intent extras I'm using eventbus to post the entire object to the next activity - activity 2. I'm exactly following the given guidelines:

PART 1: POJO

public class StudentEvent {

  public final String registrationNumber ;
  public final String name ;
  public final String course ;
  public final String branch ;

  public StudentEvent(String registrationNumber, String name, String course, String branch) {
    this.registrationNumber = registrationNumber;
    this.name = name;
    this.course = course;
    this.branch = branch;
  }

  public String getRegistrationNumber() {
    return registrationNumber;
  }

  public String getName() {
    return name;
  }

  public String getCourse() {
    return course;
  }

  public String getBranch() {
    return branch;
  }
}

PART 2: Subscription in the second activity

EventBus.getDefault().register(this); //onCreate

EventBus.getDefault().unregister(this); //onDestroy

@Subscribe
public void eventReceiver(StudentEvent studentEvent){
  tvRegistrationNumber.setText(studentEvent.getRegistrationNumber());
  tvName.setText(studentEvent.getName());
  tvBranch.setText(studentEvent.getBranch());
  tvCourse.setText(studentEvent.getCourse());
}

PART 3: Post the event

StudentEvent studentEventObject = new StudentEvent(
            etRegistrationNumber.getText().toString(),
            etName.getText().toString(),
            etCourse.getText().toString(),
            etBranch.getText().toString()) ;

 EventBus.getDefault().post(studentEventObject);

I get the Error:

D/EventBus: No subscribers registered for event class co.swisdev.abhinav.eventbustesting.StudentEvent
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.SubscriberExceptionEvent

WHAT AM I MISSING?
It is working when I make the subscription in the same class.

like image 510
Abhinav Das Avatar asked Apr 17 '16 05:04

Abhinav Das


3 Answers

Though this is not an answer, but I think this is relevant to EventBus user. Per EventBus documentation, we need to register to EventBus in onStart() and unregister in onStop()

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

@Override
public void onStop() {
   EventBus.getDefault().unregister(this);
    super.onStop();
}
like image 131
ישו אוהב אותך Avatar answered Oct 14 '22 02:10

ישו אוהב אותך


This seems to be a timing issue. Activity 2 must be registered to receive the event. If you are posting the event from Activity 1, there is no guarantee that Activity 2 has been created.

like image 11
Jesse Buss Avatar answered Oct 14 '22 01:10

Jesse Buss


You should try to use @Subscribe(sticky = true) annotation in "Activity 2", and call bus.postSticky(...) method in "Activity 1".

Documentation

like image 5
ytrikoz Avatar answered Oct 14 '22 01:10

ytrikoz