0

What Am I doing:

I have a subscriber in MainActivity.Java

I have registered in OnStart of activity

@Override
    public void onCreate() {
        super.onCreate();
        //Register the event bus for the screen
        EventBus.getDefault().register(this);

    }


@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(BusCategories event) {
        //On Click of Categories Label

}

Then, I have a Publisher in the FragmentA which is

EventBus.getDefault().post(new BusCategories(mUploadProducts.getCategories()));

So a event in Fragment is triggered calls in main activity

  1. This Is working, but say I go to power save mode in phone where onPause is triggered and come back again the subscriber never gets triggered
  2. How to properly handle this
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • I wouldn’t subscribe in `onCreate` but rather either `onStart` or `onResume` and would (respective) unsubscribe on `onStop` or `onPause` (depending which one you chose). – Martin Marconcini Aug 25 '17 at 21:49
  • @MartinMarconcini how subscribing in onStart or onResume solves the issue here? – Ritt Aug 25 '17 at 21:52
  • Until we see what the OP is doing in either method, it’s impossible to tell given the amount of provided information. However, I would still follow the practice of using the lifecycle as intended. Not to mention I wouldn’t use an event bus to begin with… – Martin Marconcini Aug 25 '17 at 21:53
  • On registering on OnStart .. does solve the issue ... but why we need to register again and again ... when we can do only once in onCreate – Devrath Aug 25 '17 at 21:55
  • Ah… that’s the topic for another question ;-) – Martin Marconcini Aug 25 '17 at 21:56
  • @Devrath when you were unregistering it before? in which life cycle? – Ritt Aug 25 '17 at 21:57
  • I was unregistering in `OnDestroy` of activity – Devrath Aug 25 '17 at 22:01
  • Strange..!! in that case, it should work. – Ritt Aug 25 '17 at 22:02
  • 1
    @Ritesh ... Sorry I was unregistering in OnStop .... that was the issue .... After unregistering in OnDestroy ... its working .... Best practice would be OnCreate .. register and OnDestroy unregister :) – Devrath Aug 25 '17 at 22:08
  • @Ritesh may this serve as a lesson not to bash answers before having all the data. My comment was actually solving the issue because I had the idea that the OP was doing exactly what he’s doing. Yet you bashed me (ironically) trying to sound smart… chill, this is a free space and we’re all trying to help. Now… you @devrath… “best practice would be on create/ondestroy” is not true. It really depends, especially if you have fragments… but if it works for you, good. It really depends. – Martin Marconcini Aug 25 '17 at 22:12
  • 2
    @MartinMarconcini Chill man..!! Check out his code, he has registered in onCreate(), and how it works if you do the same in onStart() or Stop(). I was curious so asked, as how it's possible.And, you should have answered the same instead of putting as "that's the topic for another question". And yes onCreate() and onDestory() is not a best practise, it actually depends on your usecase, normally the registering and unregistering should happen in onStart() and onStop(). And i did the right here..!! – Ritt Aug 25 '17 at 22:23
  • @Ritesh and Martin ... Thanks Guys ... This discussion helped me understand EventBus better :) – Devrath Aug 25 '17 at 22:33

2 Answers2

0

You should handle registering and unregistering in onStart and onStopmethods or onResume and onPause methods respectively.

Aalap Patel
  • 2,058
  • 2
  • 17
  • 31
0

In my case, you need to make the event nullable object if you are using Kotlin. for example :

//this will not tigger by eventbus  
@Subscribe
fun onEvent(message Short){

}


//but will trigger by event bus  
@Subscribe
fun onEvent(message Short?){

}
sourav pandit
  • 8,647
  • 2
  • 19
  • 17