Use  Event Bus over Local Broadcast Manager (Android)
Greenrobot

Use Event Bus over Local Broadcast Manager (Android)

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

  1. Its .Jar file is very small in size.
  2. It is very easy to use, You only need 3 steps to integrate it.
  3. 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)

Noman Arif

Senior Android & Flutter Developer | Experienced | Java, Kotlin, Dart | Expert in API Integration & Backend | Open for Exciting Opportunities

8 年

Next Level benefit

回复
Uferah Shafi, Ph.D

PhD in Computer Science | World's Top 2% Scientists | Computer Vision, Machine and Deep Learning Specialist

8 年

Its really helpful.

Shakeel Ahmed

CEO Inteliwaretech

8 年

too much helpful

要查看或添加评论,请登录

Muhammad Intsab Haider的更多文章

  • FaceApp Controversy

    FaceApp Controversy

    From past couple of days, I am watching the controversy against #FaceApp on many Television Channels and Articles, That…

    1 条评论
  • Android Wireless Debugging

    Android Wireless Debugging

    For an android developer mobile data cable is very important for debugging or running project on real device, but in…

  • Image Sending new Technique Suggestion!

    Image Sending new Technique Suggestion!

    Some times we don't have knowledge to discuss about something but we have some questions in our mind who makes us…

    4 条评论
  • Android studio vs Eclipse

    Android studio vs Eclipse

    Some one Asked me that "I am new on android and what should i select Android Studio or Eclipse?" I replied that Go to…

    6 条评论
  • Why Not To Share APK File of your Android Project.

    Why Not To Share APK File of your Android Project.

    IF YOU ARE ANDROID DEVELOPER (Read me..

    2 条评论
  • Rooting Android and its Risks.

    Rooting Android and its Risks.

    First off, what’s rooting? Rooting is a process that allows you to attain root access to the Android operating system…

社区洞察

其他会员也浏览了