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
47
votes
4 answers

Is using event library like Otto or EventBus a recommended way to handle relations between Activities, Fragments, and background threads

In most of the case, when dealing with case User thread (AsyncTask) to perform background processing Pass back calculated result back to Activity or Fragment Activity or Fragment re-creation might happen before user thread finishes its background…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
46
votes
15 answers

EventBus - Subscriber class and its super classes have no public methods with the @subscribe annotation

I'm creating an Android application using EventBus for posting asynchronous broadcasts to other classes, but I'm running into an error during execution. MainActivity.java import android.content.Intent; import…
Rahul Kulhalli
  • 555
  • 1
  • 5
  • 12
24
votes
4 answers

Which Activity lifecycle methods are best to register/unregister to event bus?

What is the best place to register and unregister to an event bus (like otto, EventBus, or tinybus) in an Activity and why? onCreate()-onDestroy() onStart()-onStop() onResume()-onPause() Otto's example uses onResume()-onPause(), EventBus's…
levavare
  • 494
  • 1
  • 4
  • 13
23
votes
2 answers

Can I use greenrobot EventBus for Communication between Activity and Service?

EventBus Can I use this library for Activity to Service communication ? I have tried this in my app as follows: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); …
20
votes
3 answers

Is it good to replace broadcast receiver with Greenrobot Eventbus for triggering event based functions and data transfer from service to activity?

I have implemented a service, where I handle the state changes(connect, disconnect, onServiceDiscoverd, onCharacteristicChange etc) and receiving data from another device through gatt Server. My question is, Can the events be handled efficiently…
17
votes
4 answers

Differences between Greenrobot EventBus and Guava's EventBus

I´ve been using quite often EventBus from greenrobot https://github.com/greenrobot/EventBus But I´ve just realised that Guava has its own EventBus com.google.common.eventbus.EventBus Does someone know if there are big differences?
Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60
16
votes
1 answer

EventBus, register and registerSticky method

I use greenrobot EventBus library to send data between two fragments in my android app and I want to know what is the diffeence between register(Object b) method and registerSticky(Object object) method?
karoluch
  • 653
  • 3
  • 9
  • 21
15
votes
2 answers

GreenRobot EventBus error in fragments: No subscribers registered for event class

I have an activity, its layout contains a FrameLayout. I use the framelayout as a fragment container. I replace the fragments in the FrameLayout using FragmentManager transactions. In one of the fragment's onCreate method I register the fragment…
GunnerFan
  • 3,576
  • 3
  • 25
  • 38
14
votes
4 answers

Otto/EventBus across multiple processes

Is it possible to post event in one process (for example inside SyncAdapter which has android:process=":sync" manifest attribute) and receive it in another (inside regular app UI) with Otto or EventBus? I know that Intent and BroadcastReceiver work…
svenkapudija
  • 5,128
  • 14
  • 68
  • 96
11
votes
2 answers

GreenRobot EventBusException: Subscriber class already registered to event class

I have a MainActivity and a Service in my android app. I have registered both to the EventBus and when I start the service from the main activity manually via a switch everything works fine. However, when I start the service from an AlarmManager…
nwhess21
  • 121
  • 1
  • 1
  • 6
11
votes
2 answers

Communicating from Thread to Thread using GreenRobot EventBus

Just started with GreenRobot's EventBus. There is only one thing that keeps me struggling: How do you communicate between different threads so that the onEventXY() method is actually also executed within the subscribed thread. It seems that when…
user504342
  • 945
  • 2
  • 16
  • 36
11
votes
2 answers

Threading events using GreenRobot EventBus

I've just started looking at GreenRobot's EventBus for Android and have a question about threading. I have a long-running process that I'd like to run on a background thread which, when completed, updates the UI. So something like: public void…
jFort
  • 327
  • 6
  • 17
10
votes
4 answers

how to use greenrobot to pass data to activity or fragment that has not been initialized yet?

I tried to use greenrobot pass data between activities and fragment,but I couldn't find a suitable tutorial that show how do it in detail. Based on what I have read so far I wrote some thing like this,but it doesn't work.how can I use green robot to…
armin
  • 1,985
  • 6
  • 38
  • 52
9
votes
1 answer

EventBus - @Subscribe annotated method is never used

When using the greenrobot EventBus library, all @Subscribe-annotated methods are displayed in light grey with a warning Method onMyEvent() is never used in Android Studio. Is there a way to automatically suppress this warning (as the method is not…
friederbluemle
  • 33,549
  • 14
  • 108
  • 109
9
votes
3 answers

Kotlin: Can we use @Subscribe of EventBus (GreenRobot) in Kotlin?

My onEvent in a fragment as below, capturing the authentication of the activity, in my Kotlin function. However, I can't get that onEvent triggered. @Subscribe fun onEvent(event: AuthenticationEvent) { if (event.isAuthenticated) { …
Elye
  • 53,639
  • 54
  • 212
  • 474
1
2 3
16 17