Overview

Chapter 4. Connecting the user interface with networking

This chapter shows how to connect Android UI events to networked data flows and back to the screen using RxJava. It begins by clarifying the lifecycle of subscriptions: subscribing creates a Disposable that represents the relationship between a source and a consumer, which ends either when the source completes or when you dispose it. The text also explains RxJava 2’s subscribe conventions (Observer callbacks, consumer-based overloads that return a Disposable, and LambdaObserver), and frames the developer as the author of the graph that defines how observables and observers interact, rather than as either endpoint itself.

To make the concepts concrete, the chapter builds a Flickr search client. It starts with a simple, hardcoded search and a basic RecyclerView render, then moves to a button-driven search and exposes the pitfalls of ad hoc, nested subscriptions—especially overlapping requests when a user triggers multiple searches. The recommended approach is to keep explicit subscriptions centralized (ideally one at the end where results are rendered), let the chain manage intermediate lifecycles, and, when needed, use helpers like SerialDisposable (to replace ongoing work with new work) and CompositeDisposable (to bundle related disposables) in a controlled way.

The core solution is a reactive chain that turns button clicks into network calls and then into UI updates. Clicks are mapped to the current query, switchMap starts the search while canceling any previous one, and the returned list is expanded with fromIterable so each photo can trigger additional asynchronous work. Per-item details are fetched with operators such as concatMap (to sequence) or flatMap (to run concurrently), combineLatest merges results like thumbnail and username, and toList re-aggregates everything before switching back to the main thread to render. Beyond the mechanics, the chapter emphasizes modularity and resilience: complex, cascading requests can be composed safely, performance trade-offs considered, and new requirements (like adding usernames) satisfied by touching only the focused transformation step in the chain.

The Flickr App Garden

What you have so far

1. The user clicks the Search button, triggering a request to the Flickr API

2. The user starts a new search before the first one returns from the Flickr API

3. The network request finishes without further interruption

FAQ

What is a subscription in RxJava 2 and how do you manage it?A subscription is represented by a Disposable. You get it either from subscribe overloads that return a Disposable or via Observer.onSubscribe(Disposable). You manage its lifecycle by calling dispose() to cancel, and you can check isDisposed(). Subscriptions also end automatically when the source completes or errors.
When does a subscription terminate?Two common ways: (1) the source signals completion (onComplete), or (2) you call dispose() on the Disposable. Errors (onError) also terminate the stream. After termination, the Disposable is effectively released.
If ReactiveX says subscribe returns nothing, how do RxJava 2 subscribe overloads give me a Disposable?The base subscribe(Observer) conforms to the standard and returns void, delivering the Disposable via onSubscribe. RxJava adds convenient subscribe overloads (with Consumers and lambdas) that do return a Disposable, so you can store and dispose it directly. In practice you almost always use these overloads.
Observer vs Subscriber vs LambdaObserver — what’s the difference?In RxJava 2, Observable uses Observer; Flowable uses Subscriber (with backpressure). Conceptually they’re both “subscribers.” LambdaObserver is a convenience implementation that lets you pass lambdas for onNext/onError/onComplete without an onSubscribe method.
Why is the type called Disposable and not Subscription?Subscription is reserved for Flowable’s internal contract. For Observable-based streams, RxJava 2 uses Disposable to represent the handle you can dispose to end the subscription.
How do I avoid overlapping network requests when users click Search rapidly?Model the button clicks as an Observable and use switchMap to start the network call. switchMap cancels (disposes) the previous in-flight request when a new click arrives, ensuring only the latest search runs.
What’s a good reactive way to wire a Search button and text field to a network call?Create an Observable from button clicks (RxBinding), map each click to the current text value, then switchMap that text into your Retrofit Observable. Finally observeOn(AndroidSchedulers.mainThread()) and subscribe to render results.
How do I apply an async operation to each item in a list and then collect the results?Turn the list into a stream with Observable.fromIterable(list), transform each item asynchronously (e.g., concatMap or flatMap to a per-item network call), and when all are done, gather them with toList() to emit a single List.
When should I use SerialDisposable vs CompositeDisposable?Use SerialDisposable when you always want to keep only the latest Disposable (setting a new one disposes the old automatically)—great for “replace previous request” scenarios. Use CompositeDisposable to group multiple related Disposables so you can dispose them all at once (e.g., in onDestroy()).
How do flatMap, switchMap, and concatMap differ in behavior?- switchMap: keeps only the latest inner stream; cancels previous ones (ideal for user-driven searches).
- flatMap: merges results from all inner streams concurrently; order is not guaranteed.
- concatMap: queues inner streams and processes them one after another; preserves order but prevents parallelism.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • RxJava for Android Developers ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • RxJava for Android Developers ebook for free