RxJava for
Android App
Development
A Quick Look for Developers
K. Matt Dupree
Additional
Resources
4 Easy Ways to Learn More and Stay Current
Programming Newsletter
Get programming related news and content delivered weekly to your inbox.
oreilly.com/programming/newsletter
Free Webcast Series
Learn about popular programming topics from experts live, online.
webcasts.oreilly.com
O’Reilly Radar
Read more insight and analysis about emerging technologies.
radar.oreilly.com
Conferences
Immerse yourself in learning at an upcoming O’Reilly conference.
conferences.oreilly.com
©2015 O’Reilly Media, Inc. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. #15305
RxJava for Android App
Development
K. Matthew Dupree
RxJava for Android App Development
by K. Matt Dupree
Copyright © 2015 O’Reilly Media, Inc.. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA
95472.
O’Reilly books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (http://safaribooksonline.com ). For
more
sales department:
800-998-9938 or corporate@oreilly.com.
corporate/institutional
information,
contact our
Editor: Meghan Blanchette
Production Editor: Nicole Shelby
Copyeditor: Kim Cofer
Interior Designer: David Futato
Cover Designer: Randy Comer
Illustrator: Rebecca Demarest
October 2015:
First Edition
Revision History for the First Edition
2015-09-28: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491939338 for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. RxJava for
Android App Development, the cover image, and related trade dress are trademarks
of O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher and
the author disclaim all responsibility for errors or omissions, including without limi‐
tation responsibility for damages resulting from the use of or reliance on this work.
Use of the information and instructions contained in this work is at your own risk. If
any code samples or other technology this work contains or describes is subject to
open source licenses or the intellectual property rights of others, it is your responsi‐
bility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-93933-8
[LSI]
Table of Contents
An Introduction to RxJava. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
Sharp Learning Curve, Big Rewards 1
Observables 3
Observers 4
Observable Creation and Subscribers 6
Schedulers 8
Operators 10
Conclusion 13
RxJava in Your Android Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
RxJava and the Activity Lifecycle 15
Why RxJava-based Solutions Are Awesome 21
Conclusion 29
The Future of RxJava for Android Development. . . . . . . . . . . . . . . . . 31
Further Reading for RxJava 31
Future Directions for Android App Development with
RxJava 32
iii
An Introduction to RxJava
Sharp Learning Curve, Big Rewards
I was pretty much dragged into RxJava by my coworkers...[RxJava] was
a lot like git...when I first learned git, I didn’t really learn it. I just spent
three weeks being mad at it...and then something clicked and I was like
‘Oh! I get it! And this is amazing and I love it!' The same thing hap‐
pened with RxJava.
—Dan Lew1
As Dan Lew, a Google Developer Expert Android Developer, points
out in the preceding quotation, RxJava can be very difficult to learn.
This is unfortunate because, for reasons I point out in the next chap‐
ter, RxJava can make asynchronous data handling in Android apps
much cleaner and more flexible. In this chapter, I provide a basic
introduction to RxJava.
If you are skeptical that RxJava is worth learning about, given its
steep learning curve, skip ahead to the second section of the next
chapter. In that section, I go over a situation in which RxJava pro‐
vides us with advantages over traditional ways of handling asyn‐
chronous data in Android applications. Although you won’t under‐
stand exactly how the code in that section works, you will be able to
see how RxJava makes quick work of tasks that can often become
messy and inflexible when handled without RxJava. After seeing
how much cleaner RxJava can make your Android code, hopefully
you will have the motivation to return here to this introduction.
1 Fragmented podcast, Episode 3, “The RxJava Show,” 32:26-32:50.
1