Androideventbus new features: Add sticky events, do not need to manually unregister the event bus

Source: Internet
Author: User
Tags eventbus

just looked under the author released the new version Androideventbus v1.0.4 version, found that a lot of good things, the biggest two features is the addition of sticky sticky events and no manual logoff events
Androideventbus's GitHub address is: GitHub

The following is the author of the viscous sticky event application scene, feel quite to force, here directly paste copied

User.java class:

// 实体类实现序列化publicclass User  {        String name ;        String phoneNum;        // 其他字段省略        publicUser(String aName) {            name = aName ;        }        // 代码省略 }

First, the user class does not need to implement a serialization interface and avoids those boilerplate code. You can then publish the user object directly as a sticky event in mainactivity.

 Public  class mainactivity extends Activity {    //One click event    @Override      Public void OnClick(View v) {User Auser =NewUser ("Mr.simple"); Auser.phonenum ="123456";//Other data        //Post sticky eventsEventbus.getdefault (). Poststicky (Auser);//Jump to Profileactivity pageIntent Intent =NewIntent ( This, Profileactivity.class);    StartActivity (Intent); }}

Finally, let's see how profileactivity receives the data.

 Public  class profileactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_profile);//Register in sticky formEventbus.getdefault (). Registersticky ( This); }@Subscriber    Private void Receiveuser(User info) {//Implement your logic here, info is the user object passed over}}

In profileactivity we register profileactivity itself as a subscriber to the bus, and Profileactivity receives the sticky event posted above, which is the user object. In this case, the Receiveuser function in profileactivity is triggered, and the info parameter is the user information object of the sticky event, which realizes its logic in Receiveuser.

Yes! We did not unregister subscribers in Ondestory, that is, we did not call Eventbus's unregister () function, which is one of the newest features and the only event bus library that does not require manual logoff at this time.

It is important to note that sticky sticky events require you to manually remove them!

Androideventbus new features: Add sticky events, do not need to manually unregister the event bus

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.