Passing a Simple Event

suggest change

The first thing we need to do it add EventBus to our module’s gradle file:

dependencies {
    ...
    compile 'org.greenrobot:eventbus:3.0.0'
    ...
}

Now we need to create a model for our event. It can contain anything we want to pass along. For now we’ll just make an empty class.

public class DeviceConnectedEvent
{
}

Now we can add the code to our Activity that will register with EventBus and subscribe to the event.

public class MainActivity extends AppCompatActivity
{
    private EventBus _eventBus;

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        _eventBus = EventBus.getDefault();
    }

    @Override
    protected void onStart ()
    {
        super.onStart();
        _eventBus.register(this);
    }

    @Override
    protected void onStop ()
    {
        _eventBus.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceConnected (final DeviceConnectedEvent event)
    {
        // Process event and update UI
    }
}

In this Activity we get an instance of EventBus in the onCreate() method. We register / unregister for events in onStart() / onStop(). It’s important to remember to unregister when your listener loses scope or you could leak your Activity.

Finally we define the method that we want called with the event. The @Subscribe annotation tells EventBus which methods it can look for to handle events. You have to have at least one methods annotated with @Subscribe to register with EventBus or it will throw an exception. In the annotation we define the thread mode. This tells EventBus which thread to call the method on. It is a very handy way of passing information from a background thread to the UI thread! That’s exactly what we’re doing here. ThreadMode.MAIN means that this method will be called on Android’s main UI thread so it’s safe to do any UI manipulations here that you need. The name of the method doesn’t matter. The only think, other that the @Subscribe annotation, that EventBus is looking for is the type of the argument. As long as the type matches it will be called when an event is posted.

The last thing we need to do it to post an event. This code will be in our Service.

EventBus.getDefault().post(new DeviceConnectedEvent());

That’s all there is to it! EventBus will take that DeviceConnectedEvent and look through its registered listeners, look through the methods that they’ve subscribed and find the ones that take a DeviceConnectedEvent as an argument and call them on the thread that they want to be called on.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


GreenRobot EventBus:
* Passing a Simple Event

Table Of Contents
2 Gradle
5 Intent
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
94 GreenRobot EventBus
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard