0

I am having a few issues with the android lifecycle and Eventbus. https://github.com/greenrobot/EventBus

Currently, I am registering the activity to the bus in the onCreate lifecycle callback.

I am also unregistering it at the onStop callback. And then I am registering it on the onRestart.

When I hit the home button from my application, and then relaunch the application, it logs that onRestart is being called. However, after this, when I try to do something it gives me:

01-24 05:23:44.504: D/Event(3134): No subscribers registered for event class com.garrrila.iconolist.events.IconSelectorEvent.

How is this happening?

My onRestart code:

@Override
public void onRestart(){
    Log.d(TAG, "onRestart called.");
    super.onRestart();
    EventBus.getDefault().register(this);
}

EDIT****

I am an idiot it turns out.

Didn't call super before unregistering or registering and did it after on accident.

AnnonAshera
  • 399
  • 1
  • 4
  • 12
  • Just to add, do add an check if the class is already registered with green robot, else you will end up getting multiple events. – Jalpesh Jun 03 '16 at 05:56

3 Answers3

0

Looking at this answer OnRestart vs. OnResume - Android Lifecycle Question, i would suggest to suscribe inside the onStart() and unsuscribe on onStop() and avoid using onRestart()

Look at the lifecycle graph

EDIT

Analyzing the link, i would suggest moving from onStart to onCreate, as the answer there makes clear if you move between activities, those are recreated (you are not using fragments, did you?)

Community
  • 1
  • 1
  • I did this, moved my subscribing to onStart() and onStop(). I am not using onRestart(). However, I still get the no subscribers issue. I can see onStart() firing and but somehow it isn't registering. Even though the code is there. – AnnonAshera Jan 24 '15 at 05:46
0

Please make some stub method public void onEvent(Object obj) in anywhere if you do not expect receiving any event. Just follow the design.

RichWXD
  • 1
  • 1
0

you should call register() before super in onStart(), onStop(), onPause()

Like this,

@Override
public void onStart() {
    //Log.e(TAG, "onStart");
    EventBus.getDefault().register(this);
    super.onStart();
}

@Override
public void onStop() {
    //Log.e(TAG, "onStop");
    EventBus.getDefault().unregister(this);
    super.onStop();
}

@Override
public void onPause() {
    //Log.e(TAG, "onPause");
    EventBus.getDefault().unregister(this);
    super.onPause();
}
Jaymin Panchal
  • 2,797
  • 2
  • 27
  • 31
  • hello, why did you unregister before the super.onPause and super.onStop? Thanks for the explanation. I'm trying to understand how to do register/unregister – Woppi May 16 '17 at 14:09