Android development will be used in a succession of this open source Library Eventbus. The Eventbus is a publish/Subscribe event bus optimized for Android. 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. Download Eventbus's class library source code:Https://github.com/greenrobot/EventBus. Let's talk about simple usage.
Download the project resources in this article:
First, define a message entity class Mainsendevent
Package com.example.eventbusdemo;/** * Event message entity class * @author Mmxs * */public class Mainsendevent {protected String mstrmsg;pub Lic mainsendevent (String msg) { mstrmsg = msg;} Public String Getstringmsgdata () {return mstrmsg;}}
Second, mainactivity
Package Com.example.eventbusdemo;import De.greenrobot.event.eventbus;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;import Android.widget.toast;import Android.app.activity;import android.content.Intent; public class Mainactivity extends Activity implements onclicklistener{@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Event Registration Eventbus.getdefault (). Register (this); Initui ();} private void Initui () {button button = (button) Findviewbyid (R.id.button1); Button.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.button1:intent Intent = new Intent (); Intent.setclass ( Mainactivity.this, Twoactivity.class); startactivity (intent); break;default:break;}} The event accepts public void Oneventmainthread (Mainsendevent event) {if (event! = null) {Toast.maketext (Getapplicationcontext (), " MainactivitY Accept Data "+ event.getstringmsgdata (), Toast.length_long). Show (); TextView TextView = (TextView) Findviewbyid (r.id.textview1); Textview.settext (Event.getstringmsgdata ());}} @Overridepublic void OnDestroy () {//unregister eventbus.getdefault (). Unregister (this); Super.ondestroy ();}}Third, twoactivity
Package Com.example.eventbusdemo;import De.greenrobot.event.eventbus;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button ;/** * Second twoactivity send event * @author MMSX * */public class Twoactivity extends Activity implements onclicklistener{@Overrid eprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_two); Button button = (button) Findviewbyid (R.id.button1); Button.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.button1://event is sent Eventbus.getdefault (). Post (new Mainsendevent ("from Twoactivity msg"); break;default:break;}}}
iv. layout of two activity
1, Activity_main
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" > <button android:id= "@+id/button1" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "second activity"/> <textview android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:text=" TextView "/></linearlayout>
2, Activity_two
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" > <button android:id= "@+id/button1" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "mainactivity"/></linearlayout >
On top of that, the overall practice is to deliver messages between activity. We went in first, mainactivity. Then press button to enter a second activity. In the second activity, click the button to send the data to mainactivity, pop up with Toat, and SetText. You can see the SetText effect when you return to mainactivity.
Five
Six, the simple process
1. Event Registration or subscription
Eventbus.getdefault (). Register (this);
2, Event registration or acceptance after the subscription
public void Oneventmainthread (Mainsendevent event) {if (event! = null) {Toast.maketext (Getapplicationcontext (), " Mainactivity accepts data "+ Event.getstringmsgdata (), Toast.length_long). Show (); TextView TextView = (TextView) Findviewbyid (r.id.textview1); Textview.settext (Event.getstringmsgdata ());}}
3. Event registration or subscription cancellation
Eventbus.getdefault (). Unregister (this);
This above 3 points is together, the same page.
4, the sender of the event
Eventbus.getdefault (). Post (new Mainsendevent ("from Twoactivity msg"));
Vii. Articles of reference
http://blog.csdn.net/harvic880925/article/details/40660137
http://blog.csdn.net/lmj623565791/article/details/40920453
At last
Project Resources Download:
Reprint please indicate the source of the blog site: http://blog.csdn.net/qq_16064871
If there is a reprint does not indicate the source of the blog URL, or did not get the author's consent. The author holds copyright ownership.
Android Eventbus Publish/Subscribe to Event Bus