New Features of AndroidEventBus: Adding sticky events and removing the need to manually log out of the event Bus

Source: Internet
Author: User
Tags eventbus

New Features of AndroidEventBus: Adding sticky events and removing the need to manually log out of the event Bus

I just read the new version AndroidEventBus v1.0.4 released by the author and found that many good things are added. The two biggest features are the addition of sticky events and manual logout events.
AndroidEventBus github address: github

The following is the sticky event Application Scenario provided by the author. It feels awesome. paste and copy it here.

User. java class:

// Implement serialization of the object class public class User {String name; String phoneNum; // other fields are omitted public User (String aName) {name = aName;} // Code omitted}

First, the User class does not need to implement the serialization interface, avoiding the sample code. Then, directly publish the User object in MainActivity as a Sticky event.

Public class MainActivity extends Activity {// a click event @ Override public void onClick (View v) {User aUser = new User ("Mr. simple "); aUser. phoneNum = "123456"; // other data // publish the Sticky event EventBus. getDefault (). postSticky (aUser); // jump to the ProfileActivity page Intent intent = new Intent (this, ProfileActivity. class); startActivity (intent );}}

Finally, let's see how ProfileActivity receives data.

Public class ProfileActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_profile); // register EventBus as Sticky. getDefault (). registerSticky (this) ;}@ Subscriber private void receiveUser (User info) {// implement your logic here, and info is the passed User object }}

In ProfileActivity, we register ProfileActivity as a subscriber to the bus. In this case, ProfileActivity receives the Sticky event published above, which is the User object. In this case, the receiveUser function in ProfileActivity is triggered. The info parameter is the user information object of the Sticky event. You can implement your own logic in receiveUser.

Yes! We have not logged out the subscriber in onDestory, that is, we have not called the EventBus unregister () function. This is one of the latest features and is currently the only event bus library that does not need to be manually logged out.

Note that you need to manually remove Sticky events!

Related Article

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.