"First" learning the sticky events of the Android event bus Androideventbus

Source: Internet
Author: User
Tags eventbus

recently look at Eventbus related code, first from the use of the beginning, and then from the source point of view analysis Eventbus. Use the demo post to GitHub. Frame of Use address: Https://github.com/bboyfeiyu/AndroidEventBus

Examples of Sticky event use:

1, first of all activity or fragement to do eventbus registration and anti-registration. To send the activity of the sticky event:
  1. 1  PackageCom.example.mysimpleeventbus;2 ImportOrg.simple.eventbus.EventBus;3 Importandroid.content.Intent;4 ImportAndroid.os.Bundle;5 Importandroid.support.v7.app.ActionBarActivity;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9  Public classMainactivityextendsactionbaractivity {Ten      One     Privatebutton button; A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -button =(Button) Findviewbyid (R.id.button); -         //1 Registering the event bus first -Eventbus.getdefault (). Register ( This); +Button.setonclicklistener (NewOnclicklistener () { -              + @Override A              Public voidOnClick (View arg0) { at                 //2 sending Sticky events - Eventbus.getdefault (). Poststicky ( -                         NewUser ("Soyoungboy", "Xi ' an College of finance and Economics")); -                  -                 //Jump Page -Intent Intent =NewIntent (mainactivity. This, Otheractivity.class); in startactivity (intent); -             } to         }); +     } - @Override the     protected voidOnDestroy () { *         Super. OnDestroy (); $Eventbus.getdefault (). Unregister ( This);Panax Notoginseng     } -}


The class that receives the event: Note that registration here is not a register, but: eventbus. Getdefault (). registersticky(this);
1  PackageCom.example.mysimpleeventbus;2 ImportOrg.simple.eventbus.EventBus;3 ImportOrg.simple.eventbus.Subscriber;4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.widget.Toast;7  Public classOtheractivityextendsActivity {8 @Override9     protected voidonCreate (Bundle savedinstancestate) {Ten         //TODO auto-generated Method Stub One         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); -         //Note the registersticky here instead of the Register -Eventbus.getdefault (). Registersticky ( This); the     } -     //get the content of the Send event here - @Subscriber -     voidshowtoast (user user) { +Toast.maketext (otheractivity. This, User.tostring (), Toast.length_long). Show (); -     } + @Override A     protected voidOnDestroy () { at         Super. OnDestroy (); -         //also need unregister -Eventbus.getdefault (). Unregister ( This); -     } -}

Then configure the file:
  1. 1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3  Package= "Com.example.mysimpleeventbus"4 Android:versioncode= "1"5 Android:versionname= "1.0" >6     <USES-SDK7         android:minsdkversion= "8"8 android:targetsdkversion= " the" />9     <ApplicationTen         Android:allowbackup= "true" One Android:icon= "@drawable/ic_launcher" A Android:label= "@string/app_name" - Android:theme= "@style/apptheme" > -         <Activity the             Android:name=". Mainactivity " - Android:label= "@string/app_name" > -             <Intent-filter> -                 <ActionAndroid:name= "Android.intent.action.MAIN" /> +                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> -             </Intent-filter> +         </Activity> A         <ActivityAndroid:name=". Otheractivity " > at         </Activity> -     </Application> - </Manifest>


If you set tag when mainactivity sends the event bus, otheractivity@Subscriber Note If no tag is added, no data will be obtained unless the tag is setset the tag code as follows:
    1.  1 EventBus.getDefault().postSticky( 2 new User("soyoungboy", "西安财经学院"),"soyoungboy");  
The corresponding otheractivity is replaced by:
    1. //此处获取发送事件的内容
    2. 1 // get the content of the Send event here 2     @Subscriber (tag = "Soyoungboy")3     void  showtoast (user user) {4          Toast.maketext (otheractivity.  this, user.tostring (), Toast.length_long). Show (); 5     }

Frame corresponding to the demo has such a sentence: different components (Activity, Fragment, service, etc.) can be used to publish events through the event bus between different threads, which are thread-safe. * The event can be received as long as the parameter type and tag of the published event match.  

"First" learning the sticky events of the Android event bus Androideventbus

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.