EventBus学习
Git位置https://github.com/greenrobot/EventBus
使用起来很方便:
1. Implement any number of event handling methods in the subscriber:
public void onEvent(AnyEventType event) {}
2. Register subscribers:
eventBus.register(this);
3. Post events to the bus:
eventBus.post(event);
4. Unregister subscriber:
eventBus.unregister(this);
5. public void onEventMainThread(EventType event)方法
两个Activity直接的使用
在第一个Activity的Code如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
EventBus.getDefault().register(this);
textView = (TextView)findViewById(R.id.textView);
Button btn_try = (Button)findViewById(R.id.btn_try);
btn_try.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
}
});
}
@Subscribe
public void onEventMainThread(EventType event){
textView.setText(event.getMessage());
Toast.makeText(this,event.getMessage(),Toast.LENGTH_LONG).show();
}
OnDestory方法
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
在第二个Activity中使用
Button btn_first_event = (Button)findViewById(R.id.btn_first_event);
btn_first_event.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new EventType("FistEvent btn cliced"));
}
});
具体实现可参考。
1、《EventBus使用详解(一)——初步使用EventBus》
2、《EventBus使用详解(二)——EventBus使用进阶》
其它参考:
《Android解耦库EventBus的使用和源码分析》:http://blog.csdn.net/yuanzeyao/article/details/38174537
原理很重要哦。
《EventBus的使用初试》:http://blog.csdn.net/pp_hdsny/article/details/14523561
《EventBusExplained 》:https://code.google.com/p/guava-libraries/wiki/EventBusExplained
EventBus学习的更多相关文章
- EventBus学习入门
EventBus Features What makes greenrobot's EventBus unique, are its features: Simple yet powerful: Ev ...
- EventBus学习笔记(一)
EventBus是Android和Java的发布/订阅事件总线 EventBus分三个步骤 1.定义事件 public static class MessageEvent { /* Additiona ...
- Android组件间通信库EventBus学习
项目地址: https://github.com/greenrobot/EventBus EventBus主要特点 1. 事件订阅函数不是基于注解(Annotation)的,而是基于命名约定的,在 ...
- EventBus 及一些思考
EventBus 是 Android 开发的一种常用框架,其解耦的思维令人赞叹 从特性上来讲,其与 Android SDK中的BroadcastReceiver很像,二者都是注册,发送事件,反注册,都 ...
- Android学习系列(43)--使用事件总线框架EventBus和Otto
事件总线框架 针对事件提供统一订阅,发布以达到组件间通信的解决方案. 原理 观察者模式. EventBus和Otto 先看EventBus的官方定义: Android optimized event ...
- Guava包学习--EventBus
之前没用过这个EventBus,然后看了一下EventBus的源码也没看明白,(-__-)b.反正大概就是弄一个优雅的方式实现了观察者模式吧.慢慢深入学习一下. 观察者模式其实就是生产者消费者的一个变 ...
- Android 框架学习3:从 EventBus 中学到的精华
关联文章: EventBus 3.0 的特点与如何使用 源码分析 EventBus 3.0 如何实现事件总线 学习的目的是为了超越,经过前面对 EventBus 3.0 的学习,我们已经对它相当熟悉了 ...
- Android 框架学习2:源码分析 EventBus 3.0 如何实现事件总线
Go beyond yourself rather than beyond others. 上篇文章 深入理解 EventBus 3.0 之使用篇 我们了解了 EventBus 的特性以及如何使用,这 ...
- Guava源码学习(五)EventBus
基于版本:Guava 22.0 Wiki:EventBus 0. EventBus简介 提供了发布-订阅模型,可以方便的在EventBus上注册订阅者,发布者可以简单的将事件传递给EventBus,E ...
随机推荐
- css在各浏览器中的兼容问题
CSS对浏览器的兼容性有时让人很头疼,或许当你了解当中的技巧跟原理,就会觉得也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理方法并 整理了一下.对于web2.0的过度,请尽量用xhtml ...
- Linux下Find命令的使用
Find命令的一般形式为: findpathname-options[-print-exec-ok] pathname:find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录. - ...
- C#裁剪照片并保存
/// <summary> /// /// </summary> /// <summary> /// 生成缩略图 /// </ ...
- 331. Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- 2015GitWebRTC编译实录8
2015.07.20 common_video 编译通过,其对libyuv有引用[1309/1600 ] CXX obj /webrtc/common_video/libyuv/common_vide ...
- Java字段初始化的规律
class InitializeBookClass { { field=200; } public int field=100; public InitializeBookClass(int valu ...
- Mybatis学习 —— 包括所有 mybatis官网
http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases
- c 函数及指针学习 9
指针的高级应用 处理命令行参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio.h> int main(int ar ...
- jquery绑定回车键发送(登录)
$(function(){ $(document).keydown(function(e){ if(e.keyCode==13){ $ ...
- HDU-5792 World is Exploding(树状数组)
题目大意:给一个整数序列,统计四元组(a,b,c,d)的个数,满足条件1:a<>b<>c<>d:条件2:<a,b>组成一个顺序对,<c,d> ...