注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

ScopedBus / BaseActivity.java https://gist.github.com/JakeWharton/3057437  

2015-02-03 13:12:49|  分类: 安卓 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
https://gist.github.com/JakeWharton/3057437
这个老是要翻墙弄,摘抄过来

Scoped event bus which automatically registers and unregisters with the lifecycle.
package com.squareup.example;
 
public abstract BaseActivity extends SherlockActivity {
private final ScopedBus scopedBus = new ScopedBus();
 
protected ScopedBus getBus() {
return scopedBus;
}
 
@Override public void onPause() {
super.onPause();
bus.paused();
}
 
@Override public void onResume() {
super.onResume();
bus.resumed();
}
}
ScopedBus.java
package com.squareup.example;
 
public class ScopedBus {
// See Otto's sample application for how BusProvider works. Any mechanism
// for getting a singleton instance will work.
private final Bus bus = BusProvider.get();
private final Set<Object> objects = new HashSet<Object>();
private boolean active;
 
public void register(Object obj) {
objects.add(obj);
if (active) {
bus.register(obj);
}
}
 
public void unregister(Object obj) {
objects.remove(obj);
if (active) {
bus.unregister(obj);
}
}
 
public void post(Object event) {
bus.post(event);
}
 
void paused() {
active = false;
for (Object obj : objects) {
bus.unregister(obj);
}
}
 
void resumed() {
active = true;
for (Object obj : objects) {
bus.register(obj);
}
}
}

I'm assuming the lines 13 & 17 in BaseActivity the "bus" variable should be "scopedBus", right?

ScopedBus / BaseActivity.java https://gist.github.com/JakeWharton/3057437 - 和申 - 和申的个人主页

Too bad there's not a Bus interface that ScopedBus and the existing Bus class could implement. Then code interacting with the ScopedBus could be none the wiser.

ScopedBus / BaseActivity.java https://gist.github.com/JakeWharton/3057437 - 和申 - 和申的个人主页

You still need to call register on child's life cycle, since you didn't call register(this) on BaseActiivty

ScopedBus / BaseActivity.java https://gist.github.com/JakeWharton/3057437 - 和申 - 和申的个人主页
lzou commented on Dec 31, 2014

BaseActivity.class there is no bus. where it come from?



  评论这张
 
阅读(317)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016