Use Event Bus over Local Broadcast Manager (Android)
Muhammad Intsab Haider
Technical Lead || Google Certified Android Developer || Mobile Developer || Working in Tamm
While building android application we often need to send and receive events or messages from one activity to an other activity to update UI or to do some task on specific event occur or on any task completed. Android provide Broadcast receiver to handle such situations. As we are too lazy to write code or to make generic solution for that kind of problems and in this situation we search for library.
In this post i am going to introduce you a very simple library for events management, That is GreenRobot Event Bus.. Github Link
There should be a question in you mind that, Why we should use it instead of Android Broadcast receivers?? The answer is
- Its .Jar file is very small in size.
- It is very easy to use, You only need 3 steps to integrate it.
- You can pass objects of java class in event data.
Now i gona show you that how u can use this library in your Existing project.
Step 1: Simply download Jar that is easily available on internet or if you are working with Gradle add this line in your gradle file:
compile 'org.greenrobot:eventbus:3.0.0'
Now we can use this library. For more understanding lets take and example.. consider we have 2 Activities The FirstActivity and the SecondActivity. On Firstactivity there is a button and we want to notify our SecondActivity that button is clicked.. For this purpose we have to send event from FirstActivity and that event should listen by second activity.. below i am just sharing first activity code.
public class FirstActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn= (Button)findViewById(R.id.btnSendEvent)
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new EventClass (Data));
//Event has been sent with data
}
});
}
}
Now in second Activity register the event that you want to receive. When event will be received the method onMessageEvent will be called where you can do your stuff here is a sample code of SecondActivity
public class SecondActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(EventClass event) {
/* Do something */
};
}
Code example is very simple hope you will understand it easily for future queries kindly send me Email on [email protected] or comment on this post.
Thanks :)
Muhammad Intsab Haider (Mobile Application Developer)
Senior Android & Flutter Developer | Experienced | Java, Kotlin, Dart | Expert in API Integration & Backend | Open for Exciting Opportunities
8 年Next Level benefit
PhD in Computer Science | World's Top 2% Scientists | Computer Vision, Machine and Deep Learning Specialist
8 年Its really helpful.
CEO Inteliwaretech
8 年too much helpful