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 ...
随机推荐
- magento安装以及搬家的注意事项
如果你的空间可以用ssh的话,你可以在官网的wiki Moving Magento To Another Server 中看到较为详细的搬家过程. 无论你的服务器是linux系统还是windows系统 ...
- 转--->svn的使用
在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...
- iOS开发资源:几个类似Path 2.0侧滑菜单的效果实现
IIViewDeckController/ViewDeck 类似 Path 2.0 的视图左右滑动的效果,可向左或者向右顺滑的滑动.支持ARC和non-ARC,默认ARC. https://githu ...
- Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- xampp访问403 Access forbidden 解决办法
本地可以访问,换一台机子就不行了,是因为权限没有开启,安装目录xampp\apache\conf\extra内有个httpd-xampp.conf文件,打开, 最后一段是 # # New XAMPP ...
- HDU 5944 暴力
Fxx and string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)T ...
- ZOJ 1048 Financial Management
原题链接 题目大意:给出12个月的收入,求一个平均值. 解法:没什么好说的,就是一个除法. 参考代码: #include<stdio.h> int main(){ int i; float ...
- html中不要忽略一些细节
1. img必备和可选的参数都有写了上了,但是必备参数里的一个值alt没写(其实一些大型的专业门户网站其实也是有存在一些小问题的,只要我们细心一 点就能发现).虽然这样alt不写,在页面中也不会有任何 ...
- XUtils解析
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ...
- 在JSP页面下使用AJAX实现用户名存在的检测
<script type="text/javascript"> function init(){ document.getElementById ...