Android--It's time to get to know a wave of eventbus.

Source: Internet
Author: User

1, the earliest use of Eventbus in the project was at the time of last year, but I have not taken the time to record, today to simple use, first look at Eventbus definition is what

Eventbus: is a publish/Subscribe event bus.

And see what it can do.

The main function is to substitute intent,handler,broadcast for passing messages between Fragment,activity,service and threads. The advantage is that the cost is small and the code is more elegant. and decoupling the sender and receiver.

Let's simply implement its function:

Eventbus's simple and practical is the following four methods

Eventbus.register (this);//Add Bindings

Eventbus.post (event event);//Send a message to the binder

Oneventmainthread (event event);//interface callback for the message received by the binder

Eventbus.unregister (this);//Unbind

Now that you know the approximate use of the process, start our code.

Add Dependencies First:

Compile ' org.greenrobot:eventbus:3.0.0 '

Look at Mainactivity.java,secondactivity.java again, it's not too much to describe.

Package Com.qianmo.eventbustest;import Android.content.intent;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import android.view.view;import android.widget.button;import Android.widget.TextView; Import Android.widget.toast;import Com.qianmo.eventbustest.event.firstevent;import Org.greenrobot.eventbus.eventbus;import Org.greenrobot.eventbus.subscribe;public class MainActivity extends    appcompatactivity {private Button Mfirstbutton;    Private TextView Mtextcallback;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Eventbus.getdefault (). Register (this);        Mfirstbutton = (Button) Findviewbyid (R.id.btn_first);        Mtextcallback = (TextView) Findviewbyid (R.id.tv_sendcallbcak); Mfirstbutton.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view ) {startactivity (new INtent (Mainactivity.this, Secondactivity.class));    }        }); } @Subscribe public void Oneventmainthread (Firstevent firstevent) {toast.maketext (Mainactivity.this, Firstev        Ent.getmsg (), Toast.length_short). Show ();    Mtextcallback.settext (Firstevent.getmsg ());        } @Override protected void OnDestroy () {Super.ondestroy ();    Eventbus.getdefault (). Unregister (this); }}

  

Package Com.qianmo.eventbustest;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.button;import Com.qianmo.eventbustest.event.firstevent;import Org.greenrobot.eventbus.eventbus;public class Secondactivity extends Appcompatactivity {    private Button Msecondbutton;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_second);        Msecondbutton = (Button) Findviewbyid (r.id.btn_second);        Msecondbutton.setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick (view view) {                Eventbus.getdefault (). Post (new Firstevent ("I Am Interface II"));}}        )    

* After 3.0, it is necessary to add @subscribe to the Oneventmainthread.

Android--It's time to get to know a wave of eventbus.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.