Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsJul 23, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
EventBus
Comando de instalación directa
npx -y tokrepo@latest install 8d6f42d6-86d7-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con 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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados