EventBus is a publish/subscribe event bus optimized for Android.

so make it simple,just think EventBus as a framework that allow different compoents to communicate,

usually a subscribe register a certain event,then whenever a publisher has post an event,any subscribes who had register this event will be able to receive the notify.

This is an example,Let's say MainActivity is a subscribe who has register to receive RecvEvent event,

And Second Activity is a publicer who will post a RecvEvent when his btn is clicked.Theoretically speaking,MainActivity is able to recevie the event that posted by SecondActivity.


//From above,we know that EventBus contains three objects:Event,Subscribe,Publisher

//Eventpublic class RecvEvent{private final String info;public RecvEvent(String info){ this.info = info;}public String getInfo(){return info;}}//Subscribe:MainActivitypublic class MainActivity extends AppCompatActivity {   private TextView tvShowInfo;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tvShowInfo = (TextView)findViewById(R.id.tvShowRecv);//register to recevie ShowRecvEvent event        EventBus.getDefault().register(this);    }    @Override    protected void onDestroy() {        super.onDestroy();//destroy res        EventBus.getDefault().unregister(this);    }    @Subscribe(threadMode = ThreadMode.MAIN)    public void onRecvEvent(ShowRecvEvent event)    {        tvShowInfo.setText(event.getInfo());    }    public void onJump(View view)    {        startActivity(new Intent(this,SecondActivity.class));    }}//publisher:SecondActivitypublic class SecondActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.second_activity);    }    public void onSend(View view)    {        EventBus.getDefault().post(new ShowRecvEvent("this is from second activity"));    }}


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android adb命令安装和删除apk应用
  2. Android 接口定义语言AIdl
  3. Android Webview upload 图片上传
  4. [AndroidUI]使用DrawerLayout实现侧滑菜
  5. Android(安卓)在SQLite中存取二进制图片
  6. Android动画原理分析
  7. Android开发之 shape的使用
  8. Android(安卓)实现以太网口共享3G网卡连
  9. android全局异常检测并处理
  10. Android 面试(八):说说 Activity、View、Win