This chapter introduces the reactive programming mindset for Android and sets the stage for using RxJava 2 as the core toolkit. It explains why reactive approaches are widely adopted, what readers can expect to gain (thinking in data flows rather than callbacks), and where Rx helps—or hurts—depending on how it’s used. The author outlines the goals of the book: learn through concrete examples, see applications as streams of events, and understand how and when Rx fits into everyday Android development.
The chapter positions Rx among OOP, FP, and FRP, then distills reactive programming to observables that publish values and subscribers that consume them, with operators transforming data along the way. Asynchrony becomes the default rather than a special case, which helps reduce “gray code” by making edge cases explicit. A live search example demonstrates the approach: convert UI input into an observable stream, filter short inputs, debounce to wait for user pause, and ensure UI work returns to the main thread. Time-aware operators and marble diagrams illustrate how Rx can “bend time” between emission and consumption while keeping components decoupled.
Practically, the chapter clarifies prerequisites (basic Android and Java, comfort with lambdas), the development setup (Android Studio with RxJava, RxAndroid, and RxBinding), and the structure of the book (fundamentals, architecture, and larger projects). It surveys the broader reactive landscape across mobile, web, and backend, and introduces exercises and “coffee breaks” to reinforce learning. The closing message emphasizes that mastery comes from learning to frame problems as streams and choosing the right operators—what you code is what you get—leading to clearer, more maintainable Android apps.
FAQ
Why use RxJava 2 on Android?
It’s a proven, production-ready ReactiveX implementation with a consistent API across platforms. Learning its operators and patterns transfers to other languages, and on Android it helps you tame UI events, background work, and data flows with concise, composable code.How do reactive programming, FRP, and Rx relate?
Reactive programming is the umbrella idea of building apps around data flows and reactions to change. FRP (functional reactive programming) applies functional concepts to those flows. Rx (Reactive Extensions) is a practical toolkit for this style; RxJava is the Java/Android implementation.What core problem does Rx solve on Android?
Coordinating asynchronous work and UI events. Instead of special-case code for threads, callbacks, and timing, Rx models all inputs as streams, lets you transform/filter them with operators, and keeps publishers and subscribers decoupled in time and space.What is the publish–subscribe pattern in RxJava?
An event source (publisher) emits values; subscribers register to receive them. In Rx terms, an Observable emits items and a Subscriber/Observer reacts to them. You can place operators between the two to transform, combine, filter, or time-shift the events.What are Observables and Subscribers (Observers)?
An Observable is a source that emits a sequence of values over time. A Subscriber/Observer consumes those values. The same subscriber doesn’t need to know when, how often, or from where items arrive—only how to react to each emission.How does Rx make asynchronous operations easier?
Every step in a pipeline is treated as potentially asynchronous. Operators handle timing (delay, debounce), threading (observeOn/subscribeOn), error propagation, and backpressure (with specialized types), so you compose behavior instead of orchestrating threads and callbacks manually.How do I build a debounced live-search text field?
- Convert text changes to an Observable (for example, via RxBinding).- Filter out short inputs (e.g., length < 3).
- Debounce for a short window (e.g., 150 ms) to wait for a pause in typing.
- Switch to the main thread before touching the UI.
- Subscribe with a function that triggers the search/update.
How should I handle UI threading with Rx on Android?
Use observeOn(AndroidSchedulers.mainThread()) just before any operator or subscriber that updates Views. Time-based operators may do work off the main thread; observeOn ensures downstream steps run on the UI thread safely.What are marble diagrams and why use them?
They’re timelines that depict items (“marbles”) flowing through an operator from left to right. They make it easy to reason about event timing, ordering, and how operators like filter and debounce reshape a stream.What do I need before starting, and how do I set up?
- Prerequisites: basic UI development experience and the ability to read Java 8 lambdas. No prior Rx/FP expertise is required.- Setup: use Android Studio. Add dependencies for RxJava 2, RxAndroid (Android-specific schedulers), and RxBinding (to turn Android UI events into Observables). Configure Java 8 language features for lambdas in your Gradle settings.
RxJava for Android Developers ebook for free