Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Intents or an event bus to communicate within the same app

Tags:

I understand how to use Intents to communicate with the system/other apps. I understand how to use Intents within the same App. I also understand how to use Otto to communicate within the same App.

What are the Pro/Cons of using Otto vs. Intents to communicate between my Activities/Services?

like image 796
Martin S. Avatar asked Jun 26 '13 10:06

Martin S.


People also ask

What is event bus used for?

EventBus is an open-source library for Android and Java using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.

What is event bus with example?

EventBus is a library for Java and Android that simplifies communication between various components or parts of an application. For example is in android we have several components like: Activities. Fragments.

What is event bus in Java?

The EventBus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). It is designed exclusively to replace traditional Java in-process event distribution using explicit registration.


1 Answers

Pros for using Otto:

  • You get to design your own event types, versus having to use custom actions or something to distinguish one Intent from another

  • Everything is within your own process (contrast with startActivity() and kin, which always involve IPC, even if the activity you are starting is in your own app), for speed and security

  • A bit less coding, as you aren't having to instantiate IntentFilter or BroadcastReceiver objects

  • It offers the producer pattern (as a quasi-replacement for sticky broadcasts)

  • Being not part of the OS, it has the potential to be updated more frequently

Cons for using Otto:

  • It cannot start an activity

  • It cannot start a service

  • It cannot bind to a service

  • It cannot send a broadcast

  • It cannot be used in a PendingIntent or for any true IPC

IOW, the true comparison for Otto is with LocalBroadcastManager, not with the general use of Intents.

like image 153
CommonsWare Avatar answered Sep 20 '22 04:09

CommonsWare