Android -- EventBus usage

Source: Internet
Author: User

Android -- EventBus usage
EventBus is an Android-optimized publish/subscribe message bus that simplifies communication between components in the application and between components and background threads. For example, in the request network, the UI is notified through Handler or Broadcast when the network returns. The two Fragment must communicate with each other through Listener. These requirements can be implemented through EventBus. As a message bus, there are three main elements: Event: events can be any type of objects. Subscriber: Event Subscriber. It receives specific events in EventBus and uses conventions to specify event Subscriber for simplified use. That is, all event subscriptions start with onEvent. Specifically, the function names are onEvent, onEventMainThread, onEventBackgroundThread, and onEventAsync, which are related to ThreadMode. Publisher: Event Publisher, used to notify Subscriber that an event occurs. You can send an event anywhere in any thread, directly call the 'post (Object) 'method of EventBus, and instantiate the EventBus Object by yourself, however, it is better to use the default singleton: 'eventbus. getDefault () '. The function that subscribes to corresponding types of events is automatically called based on the type of the post function parameter. ThreadMode: each event subscription function is associated with a 'threadmode'. ThreadMode specifies the function to be called. There are four threadmodes: PostThread: The event processing is in the same process as the event sending, so the event processing time should not be too long, otherwise it will affect the event sending thread, this thread may be a UI thread. The corresponding function name is onEvent. MainThread: event processing is executed in the UI thread. The event processing time cannot be too long. Needless to say, it will be ANR long and the corresponding function name is onEventMainThread. BackgroundThread: event processing is performed in a background thread. The corresponding function name is onEventBackgroundThread. Although the name is BackgroundThread, event processing is performed in the background thread, however, the event processing time should not be too long, because if the thread sending the event is a background thread, the event will be executed directly. If the current thread is a UI thread, the event will be added to a queue, A thread processes these events in sequence. If the processing time of an event is too long, it will block the dispatching or processing of subsequent events. Async: event processing is executed in a separate thread, mainly used to execute time-consuming operations in the background thread. Each event starts a thread (with a thread pool ), but it is best to limit the number of threads. Different threadmodes are used according to the function names of event subscriptions. If you want to display data in the UI thread when loading data in the background thread, the subscriber only needs to name the function onEventMainThread. Use the process to define the event type: public class MyEvent {}. Define the event processing method: public void onEventMainThread to register the subscriber: EventBus. getDefault (). register (this) sends an event: EventBus. getDefault (). post (new MyEvent () Sample public class MainActivity extends ActionBarActivity {private ConditionVariable mConditionVariable; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mConditionVariable = new ConditionVariable (); EventBus. getDefault (). register (this); initUI (); startActivity (new Intent (this, TwoActivity. class); new Thread (new SleepThread ()). start () ;}@ Override protected void onDestroy () {super. onDestroy (); EventBus. getDefault (). unregister (this);} TextView mTxtOne; private void initUI () {mTxtOne = (TextView) findViewById(R.id.txt _ one);} public void onEventMainThread (EventOne eventOne) {Log. I ("MainActivity", "MainActivity, onEventMainThread"); mTxtOne. setText (eventOne. getString ();} // public void onEventAsync (EventOne eventOne) {// Log. I ("MainActivity", "onEventAsyc --->" + Thread. currentThread (). getName (); // public void onEventBackgroundThread (EventOne eventOne) {// Log. I ("MainActivity", "onEventBackgroundThread --->" + Thread. currentThread (). getName (); // public void onEventPostThread (EventOne eventOne) {// Log. I ("MainActivity", "onEventPostThread --->" + Thread. currentThread (). getName (); //} class SleepThread implements Runnable {@ Override public void run () {Log. I ("Activity", "MainActivity, SleepThread ---> begin"); mConditionVariable. A block (2000); Log. I ("Activity", "MainActivity, SleepThread ---> end"); EventBus. getDefault (). post (new EventOne ("11111") ;}} EventOne code: public class EventOne {String string; public EventOne (String string) {this. string = string ;}public String getString () {return string ;}}

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.