Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 23, 2026·3 min de lecture

EventBus — Publish-Subscribe Event Bus for Android and Java

A lightweight publish-subscribe event bus that simplifies communication between Android components like activities, fragments, and background threads.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
EventBus
Commande d'installation directe
npx -y tokrepo@latest install 8d6f42d6-86d7-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

EventBus by greenrobot is a publish-subscribe library that decouples communication between Android components. Instead of passing callbacks, interfaces, or intents between activities, fragments, and services, components simply post event objects and subscribers receive them on the thread of their choice. It reduces boilerplate and coupling in Android applications.

What EventBus Does

  • Decouples senders and receivers so components communicate without direct references
  • Delivers events to subscribers on configurable threads: main, background, async, or posting
  • Supports sticky events that are retained and delivered to future subscribers automatically
  • Provides subscriber priorities and event cancellation for ordered processing chains
  • Offers an annotation processor for compile-time subscriber indexing to avoid reflection overhead

Architecture Overview

EventBus maintains a registry of subscriber methods indexed by event type. When an event is posted, the bus looks up all registered subscribers for that event class (and its superclasses if configured), then dispatches the event on the thread specified by each subscriber's threadMode annotation. The optional subscriber index generated at compile time replaces runtime reflection with a precomputed lookup table.

Setup & Configuration

  • Add the EventBus dependency to your module-level build.gradle
  • Annotate subscriber methods with @Subscribe and specify a threadMode
  • Register the subscriber in onStart or onCreate and unregister in onStop or onDestroy
  • Enable the subscriber index by adding the EventBus annotation processor for faster startup
  • Configure EventBus via EventBusBuilder for custom error handling, logging, or strict mode

Key Features

  • Thread delivery modes (MAIN, MAIN_ORDERED, BACKGROUND, ASYNC, POSTING) for flexible threading
  • Sticky events for delivering the last event to newly registered subscribers
  • Compile-time subscriber indexing to eliminate reflection and improve startup performance
  • Event inheritance support for dispatching to subscribers of parent event classes
  • Small library size with no transitive dependencies

Comparison with Similar Tools

  • LiveData — Jetpack lifecycle-aware observable, scoped to ViewModel; EventBus is broader for cross-component communication
  • RxJava/RxAndroid — reactive streams with operators; EventBus is simpler when you only need pub-sub without stream transformation
  • Kotlin Flow — coroutine-based reactive streams; EventBus is Java-friendly and works without coroutine infrastructure
  • Otto — Square's event bus, now deprecated; EventBus remains actively maintained with more features
  • LocalBroadcastManager — Android framework component, now deprecated; EventBus is the recommended lightweight replacement

FAQ

Q: Should I use EventBus with modern Android architecture? A: EventBus remains useful for cross-cutting concerns like logout events or connectivity changes. For ViewModel-to-UI communication, LiveData or StateFlow is the standard approach.

Q: How do I avoid memory leaks? A: Always unregister in the corresponding lifecycle callback (e.g., unregister in onStop if registered in onStart).

Q: Can I use EventBus in pure Java projects? A: Yes. The core library has no Android dependency and works in any Java application.

Q: Does sticky event delivery guarantee ordering? A: Sticky events deliver the most recent event of each type. For ordered delivery, use subscriber priority with regular events.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires