RxJava
02 / 02

Single/Completable, Error Handling & RxJava on Android

Single/Completable, Error Handling & RxJava on Android

Specialized Types: Single, Completable, Flowable

Single models a stream guaranteed to emit exactly one item or an error (onSuccess/onError) — a good fit for a network call's one-time response. Completable represents an operation producing no value, only completion or error — a fire-and-forget write or a cache clear. Flowable implements Reactive Streams with backpressure strategies (buffer, drop, latest, error) for when a source emits faster than a subscriber can consume — a gap plain Observable doesn't address.

Error Handling & Retry

api.fetchUser(id)
    .retry(3)
    .onErrorReturn(e -> User.EMPTY)
    .subscribe(this::render);

Without explicit handling, onError terminates the whole chain. onErrorReturn/onErrorResumeNext let a stream recover with a fallback value or an alternate Observable instead of propagating the failure. retry (or retryWhen for custom logic) resubscribes to the source automatically — useful for transient failures like a flaky network call.

Combining Sources: zip

Single.zip (or Observable.zip) runs several independent sources concurrently and combines their results once all have emitted — useful for fetching from multiple independent APIs in parallel and combining the results into one object, rather than awaiting them strictly sequentially.

Disposal & Android Lifecycle

A subscription not disposed when a short-lived component (an Activity/Fragment) is destroyed can leak that component or crash delivering a callback to something that no longer exists — a classic pitfall, commonly avoided via a CompositeDisposable cleared in onDestroy.

RxJava vs. Kotlin Coroutines/Flow

RxJava was widely adopted on Android to avoid nested callback hell for async work and UI events. Kotlin coroutines with Flow now offer similar capabilities with more idiomatic Kotlin syntax (suspend functions, structured concurrency) and have become the more common default for new Kotlin-first projects — RxJava remains common in established codebases and cross-platform/Java contexts.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free