Questions tagged [greenrobot-eventbus]

EventBus is an Android optimized publish/subscribe event bus that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

EventBus is an Android optimized publish/subscribe event bus. A typical use case for Android apps is gluing Activities, Fragments, and background threads together. Conventional wiring of those elements often introduces complex and error-prone dependencies and life cycle issues. With EventBus propagating listeners through all participants (e.g. background service -> activity -> multiple fragments or helper classes) becomes deprecated. EventBus decouples event senders and receivers and thus simplifies communication between app components. Less code, better quality. And you don't need to implement a single interface!

General Usage and API:

In EventBus, subscribers implement event handling methods and register themselves to the bus. Posted events are delivered to matching event handling methods based on their event type (the Java class/interfaces implemented by the event).

Using EventBus takes four simple steps:

  1. Implement any number of event handling methods in the subscriber:

    public void onEvent(AnyEventType event) {}

  2. Register subscribers:

    eventBus.register(this);

  3. Post events to the bus:

    eventBus.post(event);

  4. Unregister subscriber:

    eventBus.unregister(this);

More info: https://github.com/greenrobot/EventBus

247 questions
2
votes
4 answers

Android NavigationView onDestroy not called

I have a layout with navigation view. The view hierarchy is explained below, Navigation View> A > A1 Navigation View> B Now while I am in Fragment A1 if I open Fragment B or Fragment A by clicking the menu in navigation view the onStop or…
2
votes
1 answer

Do GreenRobot's and Guava's EventBus use reflection?

Our Android app currently uses Otto EventBus, which uses reflection. We want to avoid the overhead of reflection, but keep the flexibility. Does Guava's event bus use reflection? What about GreenRobot's? If they don't do they use code generation…
Ginandi
  • 843
  • 8
  • 20
2
votes
2 answers

How to pair retrofit with event bus

Im trying to pass data between fragments and retrofit services with usage of event bus. Some conditions which can make things a bit complicated: Fragment1 and Fragment2 can request identical data so only one request to server should be…
Alexander Zar
  • 325
  • 3
  • 14
2
votes
2 answers

EventBus, is it possible to get the sticky event in the onCreate?

I am sending data from lets say SplashActivity to main activity using EventBus's postSticky method, and depending on that data am creating tabs in MainActivity using ViewPager. So far it works great and I can setup ViewPager in @Subscribe…
Atiq
  • 14,435
  • 6
  • 54
  • 69
2
votes
1 answer

EventBus on Android: how to implement dynamic queues vs. class-based event subscription?

I want to use EventBus (by Greenrobot, or any other) for the communication between the components of my Android application. In all the example, the pub/sub was implemented using a "class" as a "topic", i.e. each subscriber declares the exact class…
Max
  • 643
  • 11
  • 27
2
votes
1 answer

EventBus posts to unregistered subscriber

While using EventBus, I have faced an unexpected situation that a subscription method of a Fragment is called one time even after the Fragment is unregistered. Scenario is like this. I have an Activity containing a layout which any Fragment can be…
neokim
  • 80
  • 10
2
votes
1 answer

No subscribers for events posted from background service

I post EventBus.getDefault().post(new SendPlayer(player)); from a services, which is running in non main thread:
2
votes
1 answer

Event Bus Index

Where/when should I add the index for Green Robot Event Bus library: EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus(); as described here? I've put this onCreate() method of my launcher activity, but sometimes I…
Jumpa
  • 4,319
  • 11
  • 52
  • 100
2
votes
3 answers

EventBus does not register event on new activity

I have implemented an otto bus example. It works fine, but ONLY on the second time I visit the activity. For example, when I load the app and hit the secret message button I am taken to the activity but the toast does not show. Then I hit the back…
hpCode
  • 51
  • 1
  • 5
2
votes
1 answer

EventBus OnEvent() is not getting called

Hi Iam using Event bus to pass data from one fragment to another Fragment From fragment-1 I am doing as below @Override public void onPause() { bsValues = new BoreShaftValues(strtext, strtextshaft); bus.post(bsValues); …
hari86
  • 659
  • 2
  • 16
  • 28
2
votes
0 answers

Android : Updating GUI from service

What I want? I am looking forward to use service in android and I want to update GUI and also use database from the service. I came to know about the library called EVENTBUS by greenrobot, but I don't know if it is safe to use. Why I want? Actually…
Ravi
  • 960
  • 1
  • 18
  • 41
2
votes
1 answer

EventBus throws No public method onEvent(Message message) when I register

I have this pretty straight forward declaration @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { EventBus.getDefault().unregister(this); …
Lester
  • 283
  • 7
  • 14
2
votes
1 answer

How to use GreenRobot's EventBus in broadcasting events from Service to Activity?

Recently I became aware of EventBus Library. Basically my use case revolves around a service and an Activity. Service is used for tracking the changes in the BLE connection. Activity is used for reporting that connection state to the UI. How can I…
Dinesh Ravi
  • 1,209
  • 1
  • 17
  • 35
2
votes
2 answers

Android EventBus and base class

I am trying to implement some common logic and reaction to some events in base class of all my dialogues. And registering and unregistering in EventBus, and catching some events in base class. So when I tried to instantiate an instance of derived…
TsimoX
  • 447
  • 5
  • 14
2
votes
1 answer

How to cancel subscription of EventBus events sent by itself?

I've got 2 running classes registered for EventBus events - instance A and instance B. Both of them have implemented onEvent(SampleEvent event) method for receiving EventBus.post(new SampleEvent(int foo, String bar)); from a place C. So far so…
Jakub Turcovsky
  • 2,096
  • 4
  • 30
  • 41