美文网首页
框架整理系列十二(EventBus传值/线程神器)

框架整理系列十二(EventBus传值/线程神器)

作者: I_Gisvity | 来源:发表于2017-04-19 10:16 被阅读0次

引用

  compile 'org.greenrobot:eventbus:3.0.0'

使用

Define events:

public static class MessageEvent { /* Additional fields if needed */ }
Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};
Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }
Post events:

 EventBus.getDefault().post(new MessageEvent());

教程

http://blog.csdn.net/harvic880925/article/details/40660137

福利


相关文章

网友评论

      本文标题:框架整理系列十二(EventBus传值/线程神器)

      本文链接:https://www.haomeiwen.com/subject/cieczttx.html