Simple EventBus - No subscribers Registered

Abhinav Das picture Abhinav Das · Apr 17, 2016 · Viewed 14.7k times · Source

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.

Answer

ישו אוהב אותך picture ישו אוהב אותך · Jul 9, 2016

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();
}