项目作者: greenrobot

项目描述 :
适用于Android和Java的事件总线,可简化活动,碎片,线程,服务等之间的通信。减少代码,提高质量。
高级语言: Java
项目地址: git://github.com/greenrobot/EventBus.git
创建时间: 2012-07-16T16:55:40Z
项目社区:https://github.com/greenrobot/EventBus

开源协议:Apache License 2.0

下载


EventBus

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

Build Status
Follow greenrobot on Twitter

EventBus…

  • simplifies the communication between components
    • decouples event senders and receivers
    • performs well with Activities, Fragments, and background threads
    • avoids complex and error-prone dependencies and life cycle issues
  • makes your code simpler
  • is fast
  • is tiny (~60k jar)
  • is proven in practice by apps with 1,000,000,000+ installs
  • has advanced features like delivery threads, subscriber priorities, etc.

EventBus in 3 steps

  1. Define events:

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

    1. @Subscribe(threadMode = ThreadMode.MAIN)
    2. public void onMessageEvent(MessageEvent event) {
    3. // Do something
    4. }

    Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:

    1. @Override
    2. public void onStart() {
    3. super.onStart();
    4. EventBus.getDefault().register(this);
    5. }
    6. @Override
    7. public void onStop() {
    8. super.onStop();
    9. EventBus.getDefault().unregister(this);
    10. }
  3. Post events:

    1. 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:

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

Java projects:

  1. implementation("org.greenrobot:eventbus-java:3.3.1")
  1. <dependency>
  2. <groupId>org.greenrobot</groupId>
  3. <artifactId>eventbus-java</artifactId>
  4. <version>3.3.1</version>
  5. </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.