Android EventBus publishing/subscribing to event Bus
EventBus, an open-source library, will be gradually used for Android development. EventBus is a release/subscription event bus optimized for Android. The main function is to replace Intent, Handler, and BroadCast to transmit messages between Fragment, Activity, Service, and threads. The advantage is that the overhead is small and the code is more elegant. And decouple the sender and receiver. The following describes the simple usage.
Download project resources in this article:
1. Define a message entity class MainSendEvent
Package com. example. eventbusdemo;/*** event message entity class * @ author mmxs **/public class MainSendEvent {protected String mstrMsg; public MainSendEvent (String msg) {mstrMsg = msg ;} public String getStringMsgData () {return mstrMsg ;}}
Ii. 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 o NCreate (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 (MainActiv Ity. this, TwoActivity. class); startActivity (intent); break; default: break;} // The event accepts 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 () ;}@overridepublic void onDestroy () {// cancel EventBus registration. getDefault (). unregister (this); super. onDestroy ();}}
3. 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 {@ Overrideprotected 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 sending EventBus. getDefault (). post (new MainSendEvent (from TwoActivity msg); break; default: break ;}}}
4. layout of two activities
1. activity_main
2. activity_two
In this regard, the overall practice is to transmit messages between activities. We first go to MainActivity. Then press the button to enter the second activity. In the second activity, press the button to send data to MainActivity, and use Toat to pop up and settext. The settext effect is displayed after MainActivity is returned.
V,
6. Simple process
1. event registration or subscription
EventBus.getDefault().register(this);
2. Acceptance after event registration or 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 cancellation of subscription
EventBus.getDefault().unregister(this);
The above three points are the same page.
4. Event senders
EventBus.getDefault().post(new MainSendEvent(from TwoActivity msg));