Chapter 2. RxJava in Your Android Code
We haven’t used Otto [an Android-focused event bus library] in a year and a half, if not more...We think we found a better mechanism. That mechanism is...RxJava where we can create a much more specific pipeline of events than a giant generic bus that just shoves any event across it.
Jake Wharton1
RxJava is a powerful library. There are many situations where RxJava provides a cleaner, more flexible way of implementing a feature within our Android apps. In this chapter, I try to show why you should consider using RxJava in your Android code.
First, I show that RxJava can load asynchronous data in a way that is both efficient and safe, even in cases where the data is loaded into objects whose lifecycle we do not control (e.g., Activitys, Fragments, etc.). Second, I compare an RxJava-based implementation of a search feature for our example HackerNews client app to a solution based on AsyncTasks, Handlers, and Listeners and I try to say a little about the advantages of the RxJava-based solution.
RxJava and the Activity Lifecycle
We do not have complete control over the lifecycle of the Activitys within our apps. Ultimately, the Android framework is responsible for creating and destroying Activitys. If the user rotates a device, for example, the Activity that is currently on screen may be destroyed and re-created to load the layout appropriate for the device’s new orientation.
This feature of the Android framework requires any effective asynchronous ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access