标签:
Sticky 事件 使用例子:
1 package com.example.mysimpleeventbus; 2 import org.simple.eventbus.EventBus; 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.ActionBarActivity; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 public class MainActivity extends ActionBarActivity { 10 11 private Button button; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 button = (Button)findViewById(R.id.button); 17 // 1 首先注册事件总线 18 EventBus.getDefault().register(this); 19 button.setOnClickListener(new OnClickListener() { 20 21 @Override 22 public void onClick(View arg0) { 23 // 2 发送Sticky事件 24 EventBus.getDefault().postSticky( 25 new User("soyoungboy", "西安财经学院")); 26 27 // 跳转页面 28 Intent intent = new Intent(MainActivity.this, OtherActivity.class); 29 startActivity(intent); 30 } 31 }); 32 } 33 @Override 34 protected void onDestroy() { 35 super.onDestroy(); 36 EventBus.getDefault().unregister(this); 37 } 38 }
1 package com.example.mysimpleeventbus; 2 import org.simple.eventbus.EventBus; 3 import org.simple.eventbus.Subscriber; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.widget.Toast; 7 public class OtherActivity extends Activity { 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 // TODO Auto-generated method stub 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 //注意此处的registerSticky而不是register 14 EventBus.getDefault().registerSticky(this); 15 } 16 //此处获取发送事件的内容 17 @Subscriber 18 void showToast(User user) { 19 Toast.makeText(OtherActivity.this, user.toString(), Toast.LENGTH_LONG).show(); 20 } 21 @Override 22 protected void onDestroy() { 23 super.onDestroy(); 24 //同样需要unregister 25 EventBus.getDefault().unregister(this); 26 } 27 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.mysimpleeventbus" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="20" /> 9 <application 10 android:allowBackup="true" 11 android:icon="@drawable/ic_launcher" 12 android:label="@string/app_name" 13 android:theme="@style/AppTheme" > 14 <activity 15 android:name=".MainActivity" 16 android:label="@string/app_name" > 17 <intent-filter> 18 <action android:name="android.intent.action.MAIN" /> 19 <category android:name="android.intent.category.LAUNCHER" /> 20 </intent-filter> 21 </activity> 22 <activity android:name=".OtherActivity" > 23 </activity> 24 </application> 25 </manifest>
1 EventBus.getDefault().postSticky( 2 new User("soyoungboy", "西安财经学院"),"soyoungboy");
//此处获取发送事件的内容
1 //此处获取发送事件的内容 2 @Subscriber(tag = "soyoungboy") 3 void showToast(User user) { 4 Toast.makeText(OtherActivity.this, user.toString(), Toast.LENGTH_LONG).show(); 5 }
【第一篇】学习 android 事件总线androidEventbus之sticky事件的传递
标签:
原文地址:http://www.cnblogs.com/androidsuperman/p/4630996.html