Home / Basics / Event Bus Android Library Tutorial – Library #9
event_bus_cover

Event Bus Android Library Tutorial – Library #9

This is library #9 in this series 100 Library in Android , in the previous article we had  Fresco Android Library Tutorial. In this article we are having the Event Bus Android Library Tutorial which makes our communications Activities, Fragments, and background threads much more simple.

What’s Event Bus?

Event Bus is an open-source library which implements observer pattern or publisher/subscriber pattern, it makes our communications Activities, Fragments, and background threads much more simple. It solves the problem of loose coupling, simplify the code, remove dependencies, and speed up app development with just a few lines of code.

Event Bus Android Library Tutorial

Our App:

We’d take an example for Login User Story, which most apps can have it. So we need that, if a user loggedIn in the first screen, all other components (Activities and Fragments) know about this event and show the userName which was used to login. So we have:

  • Two activities FirstActivity, SecondActivity.. and a fragment called FragmentA.
  • The FirstActivity also contains a FrameLayout to display FragmentA into it, and it’s also responsible for Posting the Login Event.

So the steps works as follows:

  1. Define events: Create a class to represent the Login Event.
  2. Prepare subscribers: Register and Subscribe SecondActivity and FragmentA to this event.
  3. Post events: When user types his userName and press login, an event will be posted to all subscribers and it would show the userName used to login.

Technical Overview: 

Sticky event: Some events carry information that is of interest after the event is posted. For example, an event was posted before subscribing is complete. Or if you have some sensor or location data and you want to hold on the most recent values. EventBus keeps the last sticky event of a certain type in memory.

Delivery Threads: EventBus can handle threading for you: events can be posted in threads different from the posting thread. A common use case is dealing with UI changes. In Android, UI changes must be done in the UI (main) thread. On the other hand, networking, or any time consuming task, must not run on the main thread.

EventBus has four ThreadModes, you can read how to use them from here and try it in our app. 

Event Bus Android Library Tutorial

1. Creating Android Project, In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Empty Activity, call it FirstActivity and proceed.

2. Under your package name, create a new activity call it SecondActivity, Also create a new blank fragment call it FragmentA.

3. Install the library using Gradle dependency, open Application build.gradle file and add the following code under dependencies:

4. We need to define our event in Java so we can use it in our code, so under your package name, create a new class call it LoginEvent and add this code into it. It would has a String to hold the user name.

5. Time to prepare our subscribers !! We need SecondActivity and FragmentA to listen to events when its posted,

so we need to register them into the bus and implement event handling methods (also called “subscriber methods”) that will be called when an event is posted.

Lets change FragmentA layout, open fragment_a.xml and add this code, it has a textView showing the userName when user login.

Now open FragmentA.java and add this code, it has an object of EventBus, first we register this fragment into the bus in onStart method, then we implement the subscriber method which has annotation @Subscribe.

When event is posted, the textView changes to have the userName used to login.

6. Same steps for SecondActivity,, Lets change its layout, open activity_second.xml and add this code, it has a textView showing the userName when user login.

Now open SecondActivity.java and add this code, it has an object of EventBus, first we register this fragment into the bus in onStart method, then we implement the subscriber method which has annotation @Subscribe.

Here’s the trick, when loginEvent is posted from the FirstActivity, the SecondActiviy at this moment is not registered in the EventBus yet,, so it will not catch the event when its posted !!

Solution: Using sticky = true in @Subscribe method will tell the activity that, please go and get the last LoginEvent has been posted

7. Now we have prepared our subscribers, lets go to the last part “Post events”. Open the FirstActivity layout activity_first.xml and add this code which will has a FrameLayout to hold FragmentA, EditText and Button for user login, and a button to go to SecondActivity.

8. Open FirstActivity.java and this code, it will show FragmentA in a frameLayout, and when user clicks login it will post the LoginEvent to all subscribers.

Note: In the PostEvent method we are using postSticky so the SecondActivity can handle this Event as described before.

9. Run the project and test it. You should able see The Event Bus Android Library Tutorial works and the output should look like this images below, try to use bus.post(…) instead of bus.postSticky(…) and see the behavior of the SecondActivity,, also try to use different Thread Mode from these ones.

Event Bus Android Library Tutorial

Summary
Article Title
Event Bus Android Library Tutorial - Library #9
Description
Event Bus Android Library Tutorial, library which implements observer pattern or publisher/subscriber pattern makes our communications Activities, Fragments
Author
Organization
Android Gifts

About #AndroidGifts

#AndroidGifts

Check Also

Picasso Android Library Tutorial

Picasso Android Library Tutorial – Library #5

This is library #5 in this series 100 Library in Android , in the previous article we …

Leave a Reply