Using Intents or an event bus to communicate withi

2020-05-19 05:07发布

问题:

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?

回答1:

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.