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

1

25

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?

Dennisedennison answered 26/6, 2013 at 10:44 Comment(1)
This question shouldn't be closed. The resulting answer was very informative and exactly answered my question. The answer was based on fact based pros and cons, so this question didn't generate primarily opinion-based answers.Lepage
L
39

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.

Light answered 26/6, 2013 at 11:0 Comment(8)
Thanks. So compared to LocalBroadCastManager it still has many of the Pros but none of the Cons?Dennisedennison
@MartinS.: In comparison to LocalBroadcastManager, the pros are all valid except the second one (since that is also true of LocalBroadcastManager). You are correct that the cons all fall away.Light
@Light and last one too, because LocalBroadcastManager is part of support library and it is included just in your apk.Tiertza
@Light knows his onions!Excreta
Is it better than LBM?Secundines
@Bytecode: As I noted almost two years ago, when comparing Otto to LocalBroadcastManager, Otto has all of the "pros" listed in the answer except the second one (which is also true for LocalBroadcastManager). All of the cons go away, as both Otto and LocalBroadcastManager share those limitations.Light
@Light : Can I move my LBM code to EventBus? What you suggestSecundines
@Bytecode: "Can I move my LBM code to EventBus?" -- you could, but I do not know if it is worth the change. The benefits that I cite are all programming benefits, not runtime benefits.Light

© 2022 - 2024 — McMap. All rights reserved.