Comments (8)
还有种情况,如果传的参数是Map类型,还会执行N次(N>5),看源码是Map继承自多个类的子类,然后符合这些类的响应,自然就执行多次了,解决办法是只传基础类型数据,int,boolean,string及数组类型,或者改源码
from androideventbus.
我打断点看,发送事件的位置只发送了一遍,但是接收事件的位置出现了俩次
from androideventbus.
我收到三次。。
from androideventbus.
@ccclll1990 问题的原因找到了,是fragment的复用引起的,如果fragment要复用就要进行特殊的处理
from androideventbus.
@creky 我也遇见这个问题了,我直接传递父类的直接子类就解决了这个问题,谢谢了哈。
from androideventbus.
@chengzhijun0706 你好,我也是fragment出现的,怎么处理的?
from androideventbus.
@chengzhijun0706 你好,我也是fragment出现的,怎么处理的?
如果fragment存在复用的场景需要特殊处理,我是这样处理的,仅供参考(前提时发布事件的时候能够取到订阅的对象)
需要对源码进行改动
为了区分不同的fragment的对象,我想把fragment对象的hash作为tag,进行事件注册。
但是在注解中又只能使用常量。所以想了一个迂回的办法。
1.注解的时候用参数标识这个事件注册的时候需要把对象object的hash加到tag中,
2.在EventBus注册的时候识别这个参数,并把object的hash加到tag中
3.事件发布的时候将fragment对象的hash手动添加到tag中
1.首先Subscriber注解中增加一个方法
/**
*/
boolean registerWithObjectHash() default false
;
2.Eventbus注册的时候判断该参数,并注册的时候特殊处理
浏览代码容易找到Eventus注册的方法是在SubsciberMethodHunter. findSubcribeMethods
的方法中,对该方法略作改动
/**
* 查找订阅对象中的所有订阅函数,订阅函数的参数只能有一个.找到订阅函数之后构建Subscription存储到Map中
*
* @param subscriber 订阅对象
* @return
*/
public void findSubcribeMethods(Object subscriber) {
if (mSubcriberMap == null) {
throw new NullPointerException("the mSubcriberMap is null. ");
}
Class<?> clazz = subscriber.getClass();
// 查找类中符合要求的注册方法,直到Object类
while (clazz != null && !isSystemCalss(clazz.getName())) {
final Method[] allMethods = clazz.getDeclaredMethods();
for (int i = 0; i < allMethods.length; i++) {
Method method = allMethods[i];
// 根据注解来解析函数
Subscriber annotation = method.getAnnotation(Subscriber.class);
if (annotation != null) {
// 获取方法参数
Class<?>[] paramsTypeClass = method.getParameterTypes();
// 订阅函数只支持一个参数
if (paramsTypeClass != null && paramsTypeClass.length == 1) {
Class<?> paramType = convertType(paramsTypeClass[0]);
EventType eventType
if(annotation.registerWithObjectHash()){
eventType = new EventType(paramType, annotation.tag()+subscriber.toString());
}else{
eventType = new EventType(paramType, annotation.tag());
}
TargetMethod subscribeMethod = new TargetMethod(method, eventType,
annotation.mode());
subscibe(eventType, subscribeMethod, subscriber);
}
}
} // end for
// 获取父类,以继续查找父类中符合要求的方法
clazz = clazz.getSuperclass();
}
}
example:
注册时
class Fragment
...
@Subscriber(tag="testevent",registerWithObjectHash=true)
public void test(String test){
}
发布事件时
post("","testevent"+fragment.toString)
from androideventbus.
@xilost 我用了一个比较笨的办法,就是在你传递的对象中携带有一个区分页面的字段,在接受的地方判断这个字段的值是否和本页面相等就好了
from androideventbus.
Related Issues (20)
- 我发现在参数为List<T>的情况下会被调用2次 HOT 3
- 接收到数据后,界面无法更新
- 关于 @Subscriber(tag = "my_tag",mode = ThreadMode.POST) 接收不到消息 HOT 2
- EventBus 3.0 支持注解并且方法名无限制 HOT 2
- 使用jar收不到消息
- 出现错误
- 有没有设置优先级的方法啊
- AndroidEventBus的优化建议 HOT 1
- 针对README中“与greenrobot的EventBus的不同”的话题提出几点自己的看法↓↓↓ HOT 10
- 依赖生成 HOT 1
- 我的项目是分模块的 我发现无法在在模块间通讯
- 比如a模块一个页面 b模块一个页面 无法通讯
- 关于sticky事件的移除问题 HOT 1
- 在Android 4.4.4上找不到PersistableBundle
- 有人遇到eventbus重复接收的问题吗?我在系统7.0,8.0上会重复接收,但在6.0及以下是正常的,只有一次接收。 HOT 1
- 支持跨module通信吗 HOT 4
- poststick不起作用,原因是代码被注释了,请释放 HOT 3
- 一个接收方法同时接收多个tag的实现方案? HOT 1
- 比 EventBus 更高效的事件总线(BusUtils)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androideventbus.