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., Activity
s, Fragment
s, etc.). Second, I compare an RxJava-based implementation of a search feature for our example HackerNews client app to a solution based on AsyncTask
s, Handler
s, 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 Activity
s within our apps. Ultimately, the Android framework is responsible for creating and destroying Activity
s. 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 ...
Get RxJava for Android App Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.