Convert Figma logo to code with AI

greenrobot logoEventBus

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

24,672
4,658
24,672
142

Top Related Projects

47,834

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

5,166

An enhanced Guava-based event bus with emphasis on Android support.

49,988

Google core libraries for Java

17,407

A fast dependency injector for Android and Java.

17,071

An Android library for managing images and the memory they use.

Quick Overview

EventBus is a publish/subscribe event bus for Android and Java. It simplifies communication between components in your application, decoupling event senders and receivers. EventBus is optimized for Android and supports efficient event delivery and thread management.

Pros

  • Simplifies communication between components, reducing coupling
  • Optimized for performance, especially on Android
  • Supports thread management and background processing
  • Easy to integrate and use with minimal boilerplate code

Cons

  • Can make code flow harder to follow if overused
  • Potential for memory leaks if not properly unregistered
  • May introduce unexpected behavior if events are not carefully managed
  • Learning curve for proper usage and best practices

Code Examples

  1. Defining an event:
public class MessageEvent {
    public final String message;

    public MessageEvent(String message) {
        this.message = message;
    }
}
  1. Posting an event:
EventBus.getDefault().post(new MessageEvent("Hello EventBus!"));
  1. Subscribing to an event:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}
  1. Registering and unregistering subscribers:
@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

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

Getting Started

  1. Add EventBus to your project:
implementation 'org.greenrobot:eventbus:3.3.1'
  1. Define an event class:
public class MyEvent {
    public String message;
}
  1. Register your subscriber:
@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}
  1. Define a subscriber method:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(MyEvent event) {
    // Handle event
}
  1. Post an event:
EventBus.getDefault().post(new MyEvent());
  1. Unregister when appropriate:
@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

Competitor Comparisons

47,834

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

Pros of RxJava

  • More powerful and flexible for complex asynchronous operations
  • Supports a wide range of operators for data manipulation
  • Better suited for handling multiple data streams and combining them

Cons of RxJava

  • Steeper learning curve due to its complexity
  • Can be overkill for simple event-driven scenarios
  • Requires more boilerplate code for basic operations

Code Comparison

EventBus:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    // Handle event
}

RxJava:

Observable<MessageEvent> observable = Observable.create(emitter -> {
    // Emit events
});
observable.subscribe(event -> {
    // Handle event
});

EventBus is simpler and more straightforward for basic event handling, while RxJava offers more flexibility and power for complex scenarios. EventBus is easier to learn and implement quickly, but RxJava provides a more comprehensive toolkit for reactive programming.

EventBus is ideal for simple publish-subscribe patterns within an app, whereas RxJava excels in scenarios involving multiple data streams, complex transformations, and advanced error handling. The choice between the two depends on the specific requirements of your project and the complexity of your asynchronous operations.

5,166

An enhanced Guava-based event bus with emphasis on Android support.

Pros of Otto

  • Simpler API and easier to understand for beginners
  • Integrates well with other Square libraries
  • Smaller library size, potentially reducing app bloat

Cons of Otto

  • Less performant than EventBus, especially for large numbers of events
  • Lacks some advanced features like sticky events and subscriber priorities
  • No longer actively maintained (last commit in 2015)

Code Comparison

EventBus:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    // Handle event
}

Otto:

@Subscribe
public void onMessageEvent(MessageEvent event) {
    // Handle event
}

Both libraries use annotations for event subscription, but EventBus offers more flexibility with thread modes and other options. Otto's syntax is slightly simpler, but lacks advanced features.

EventBus generally provides better performance and more features, while Otto offers a simpler API and smaller footprint. However, Otto's lack of active maintenance makes EventBus a more future-proof choice for new projects.

49,988

Google core libraries for Java

Pros of Guava

  • Comprehensive utility library with a wide range of features beyond event handling
  • Backed by Google, ensuring long-term support and regular updates
  • Well-documented and widely adopted in the Java ecosystem

Cons of Guava

  • Larger library size, which may increase app size and compilation time
  • Steeper learning curve due to the extensive API and feature set
  • Not specifically optimized for event handling like EventBus

Code Comparison

EventBus:

@Subscribe
public void onEvent(MessageEvent event) {
    // Handle event
}
EventBus.getDefault().register(this);
EventBus.getDefault().post(new MessageEvent());

Guava (using EventBus):

@Subscribe
public void handleEvent(MyEvent event) {
    // Handle event
}
EventBus eventBus = new EventBus();
eventBus.register(this);
eventBus.post(new MyEvent());

While both libraries offer event handling capabilities, EventBus is more focused and lightweight, making it easier to implement for simple event-driven architectures. Guava, on the other hand, provides a broader set of utilities and may be preferred for projects requiring additional functionality beyond event handling.

17,407

A fast dependency injector for Android and Java.

Pros of Dagger

  • Compile-time dependency injection, reducing runtime errors and improving performance
  • Generates boilerplate code, reducing manual work and potential mistakes
  • Supports modular architecture and easier testing through dependency inversion

Cons of Dagger

  • Steeper learning curve due to more complex setup and concepts
  • Increased build times due to annotation processing
  • Can lead to verbose code in some cases, especially for smaller projects

Code Comparison

EventBus:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    // Handle event
}

Dagger:

@Inject
public MyClass(Dependency dependency) {
    // Constructor injection
}

Key Differences

  • EventBus focuses on event-driven communication between components
  • Dagger specializes in dependency injection and object graph management
  • EventBus is simpler to set up and use for basic scenarios
  • Dagger provides more robust architecture support for larger, complex applications

Both libraries serve different purposes but can be used together in Android development. EventBus excels in decoupling components through event-based communication, while Dagger shines in managing dependencies and creating a more modular, testable codebase.

17,071

An Android library for managing images and the memory they use.

Pros of Fresco

  • Specialized for image loading and caching in Android apps
  • Supports progressive image loading and advanced image manipulations
  • Optimized for memory usage and performance with large images

Cons of Fresco

  • Larger library size and increased app size
  • Steeper learning curve due to more complex API
  • Limited to image handling, unlike EventBus's broader event system

Code Comparison

EventBus:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    // Handle event
}

Fresco:

SimpleDraweeView draweeView = findViewById(R.id.my_image_view);
draweeView.setImageURI("https://example.com/image.jpg");

Summary

EventBus is a lightweight event bus system for Android and Java, facilitating communication between components. Fresco, on the other hand, is a powerful image loading and caching library specifically designed for Android applications.

While EventBus excels in simplifying inter-component communication, Fresco focuses on efficient image handling. EventBus has a smaller footprint and a simpler API, making it easier to integrate for general event-driven programming. Fresco offers advanced image loading capabilities but comes with a larger library size and a more complex API.

Choose EventBus for a versatile event system, and Fresco for specialized image handling in Android apps.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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:

    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.

Homepage, Documentation, Links

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.