EventBus README

The project's README from github.com/greenrobot/EventBus. As one markdown file: README.md.

EventBus

EventBus is a publish/subscribe event bus for Android and Java.

Build Status Follow greenrobot on Twitter

EventBus...

EventBus in 3 steps

  1. Define events:

    public static class MessageEvent { /* Additional fields if needed */ }
    
  2. 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);
     }
    
  3. Post events:

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

Read the full getting started guide.

There are also some examples.

Note: we highly recommend the EventBus annotation processor with its subscriber index. This will avoid some reflection related problems seen in the wild.

Add EventBus to your project

Available on Maven Central.

Android projects:

implementation("org.greenrobot:eventbus:3.3.1")

Java projects:

implementation("org.greenrobot:eventbus-java:3.3.1")
<dependency>
    <groupId>org.greenrobot</groupId>
    <artifactId>eventbus-java</artifactId>
    <version>3.3.1</version>
</dependency>

R8, ProGuard

If your project uses R8 or ProGuard this library ships with embedded rules.

For more details please check the EventBus website. Here are some direct links you may find useful:

Features

Documentation

Changelog

FAQ

License

Copyright (C) 2012-2021 Markus Junginger, greenrobot (https://greenrobot.org)

EventBus binaries and source code can be used according to the Apache License, Version 2.0.

Other projects by greenrobot

ObjectBox (GitHub) is a new superfast object-oriented database.

Essentials is a set of utility classes and hash functions for Android & Java projects.

EventBus (greenrobot/EventBus)

Searching Database…

24.7K ·GitHub
这是什么?EventBus 是一个面向 Android 和 Java 的发布/订阅事件总线。它简化了组件之间的通信——Activity、Fragment、线程、Service——并让事件的发送方和接收方解耦。
所以它是我加到自己应用里的一个库,而不是会接管整个应用的框架?是一个库:整个 jar 包非常小(约 60k)。在你现有的 Android 或 Java 项目里,凡是组件之间需要互相通信的地方,都可以把它接进去。
在我的应用里怎么用它?三步:定义事件类;声明并注解订阅者方法(可以指定线程模式),然后注册/注销它们——在 Android 上通常跟随 Activity 或 Fragment 的生命周期;最后发布事件。
怎么把它加到我的项目里?README 里提供了针对 Android 和纯 Java 项目的现成依赖片段。如果你用 R8 或 ProGuard,不需要额外配置——所需的规则已经内置打包好了。
有什么需要留意的坑吗?有——README 强烈建议搭配它的订阅者索引(subscriber index)使用 EventBus 注解处理器,这样可以避免实际使用中常见的反射相关问题。
为什么用事件总线,而不是让组件之间直接互相调用?直接互相调用会造成紧密耦合、容易出错的依赖关系,还会带来生命周期上的麻烦。EventBus 让发送方和接收方解耦,绕开了这些生命周期问题,代码也更简洁。
除了基本的发布和订阅,它还能做什么?它还有一些高级功能,比如指定投递线程(订阅者方法可以在你选择的线程模式下运行)以及订阅者优先级,这样你就能控制事件以什么方式、按什么顺序投递。
它会让我的应用变臃肿吗?README 称它速度很快,在 Activity、Fragment 和后台线程之间都有稳定的表现——而且整个库只是一个约 60k 的 jar 包。
它在真实应用里经受过考验吗?有——README 说它已在装机量超过 1,000,000,000 的应用中得到实际验证,而且这个项目从 2012 年就存在了。
Creator
greenrobot
Size
1.7 MB
Stars
24.7K
Age
14y
Commits · 90 Days