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.