O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reactive Android Programming

Book Description

Make the most of asynchronous android programming

About This Book

  • Install and set up RxJava for Android development
  • Implement the Reactive paradigm for Android programming using RxJava
  • Create cutting edge real world Android apps with Reactive programming.

Who This Book Is For

Are you an android developer trying to figure out how to use reactive paradigm for your programming needs? If yes then this is the book for you. No previous knowledge of RxJava is required.

What You Will Learn

  • Set up an environment for asynchronous that is reactive Android programming
  • Write custom observables and higher level abstractions
  • Orchestrating multiple calls using Reactive programming principles
  • Fetch remote financial data using RxJava
  • Integrate and process Twitter streams gracefully
  • Utilize Reactive programming to develop interactive and responsive Android apps
  • Create your own application to follow financial stock updates in real-time based on selected companies’ symbols
  • Integrate updates from the Twitter for those companies.

In Detail

Writing code on Android is hard. Writing a high quality code that involves concurrent and parallel tasks is even harder. Ensuring that this code will run without unforeseen race conditions is an the order of magnitude harder. RxJava is the tool that can help write code for such tasks.

In this book a novice developer will be introduced to a wide variety of tools that RxJava provides to enable them to produce robust and high-quality code for their asynchronous tasks by building a relatively simple(and high quality) application using advanced RxJava techniques to produce a high quality product.

Part 1 of the book will lead the developer through RxJava's initial setup in Android environment. In Part 2, the reader will learn RxJava 2.0 step-by-step by starting off with stock data processing and display.The developer will learn to choose appropriate Schedulers and to use Retrofit library for remote requests.In Part 3, the reader will also learn advanced topics such as adding integration to Twitter to process its streaming data by combining it with stock data.

Style and approach

This book is a step by step practical guide which will essentially teach you to set up, implement, and debug Reactive Android Code with ease.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Building your First “Hello World” RxJava Application
    1. Creating the application
      1. Dependencies
        1. Core dependencies
        2. UI dependencies
      2. Very first user interface
        1. Adding RecyclerView
        2. Stock data Value objects
      3. Using Retrolambda
        1. Setup
        2. Usage
        3. Updating the existing code
    2. Summary
  3. Understanding the Basics of RxJava
    1. Observables
      1. Hot and Cold Observables
    2. Disposables
    3. Schedulers
      1. Scheduling examples
    4. Investigating the flow of an Observable
    5. Flowable
      1. Dropping items
      2. Preserve latest item
      3. Buffering
    6. Completable, Single, and Maybe types
      1. Completable
      2. Single
      3. Maybe
    7. Summary
  4. Learning to use Retrofit by Fetching Financial Data
    1. Setting up Retrofit
    2. Making HTTP requests
      1. Service interface
      2. Service factory
      3. Creating service
    3. Transforming request data
      1. Parsing JSON
      2. Querying data
      3. Transforming JSON value objects
        1. Unwrapping nested objects
        2. Unwrapping lists
        3. Converting objects
    4. Displaying data on the UI
    5. Regular updates
    6. Handling multiple records on the UI
    7. Summary
  5. Integrating RxJava 1.0 with RxJava 2.0
    1. Differences between the versions
      1. Dependencies and package name
      2. Functional interfaces
      3. Flowable
    2. Gluing different RxJava versions
      1. Setup
      2. Converting RxJava 1.0 Observables
        1. Converting to Flowables
    3. Summary
  6. Writing Data to SQLite and Schedulers
    1. Schedulers
      1. Types of Schedulers
        1. Single
        2. Trampoline
        3. NewThread
        4. IO
        5. Computation
        6. Executor Scheduler
        7. Android Scheduler
      2. Using Schedulers
        1. subscribeOn
        2. observeOn
        3. Rules of thumb
      3. Achieving parallelism
        1. Structuring code for parallelism
    2. Writing data with StorIO
      1. Setting up StorIO
      2. Configuring StorIO
        1. Preparing constants
        2. Creating write resolvers
        3. Creating StorIOSQLite interface
      3. Data persistence flow
    3. Summary
  7. Error Handling and SQLite Data Reading
    1. Exception handling in RxJava
      1. Using subscribe()
      2. Using onExceptionResumeNext()
      3. Using doOnError()
      4. Other error processing methods
        1. onErrorResumeNext()
        2. onErrorReturn
        3. onErrorReturnItem
      5. Showing errors in Android UI
        1. Empty State Screen
        2. Toast notification
      6. Centralized error logging
        1. Central handler
        2. Using RxJava plugins
    2. Reading SQLite data with StorIO
      1. Get resolver
        1. Reading cursor columns
        2. Converting data
        3. Creating the StockUpdate object
        4. Configuring Type Mapping
      2. Offline fallback for StockUpdate entries
        1. StorIO database query
        2. Creating the StorIO Observable
        3. The final onExceptionResumeNext() block
      3. Informing the user about the failure
      4. Missing delete resolver
    3. Summary
  8. Integrating RxJava with Activity Lifecycle
    1. Android Activity Lifecycle
      1. Lifecycle review
        1. Fragment lifecycles
      2. Setting up an activity
      3. Things to know about onCreate() calls
    2. Resource leaks
      1. Memory leaks
        1. Memory leak example
      2. Leaking with Observables
        1. Lost background tasks example
        2. Paying special attention to onCreate()
    3. Cleaning up Subscriptions
      1. Using Disposable
        1. Using CompositeDisposable
      2. Utilizing the RxLifecycle library
        1. Setting up the library
        2. Binding to Activity Lifecycle
          1. Binding to Activity without subclassing
        3. Binding to the Fragment lifecycle
        4. Binding to views
      3. Updating the data fetching flow
    4. Summary
  9. Writing Custom Observables
    1. How to create custom Observables
      1. Integrating with standard Java API
      2. Integrating with Emitter API
        1. Cleaning up
    2. Reading tweets for stocks reactively
      1. Setup
        1. Getting access to Twitter
      2. Custom Observable for Twitter
        1. Configuring Twitter
        2. Listening to status updates
        3. Emitting status updates into Observable
      3. Showing tweets in the UI
        1. Integrating Twitter status updates into the Flow
        2. Updating Value Objects
          1. StockUpdate adjustments
          2. StorIO adjustments
        3. Updating layouts
        4. Other improvements
    3. Summary
  10. Advanced Observable Flows Explained
    1. Unwrapping Observables
      1. Transforming values with Map
      2. FlatMap Observables
        1. More FlatMap variations
        2. SwitchMap
    2. Passing values
      1. Tuples
        1. JavaTuples
      2. Custom classes
      3. Updated flow
    3. Combining items
      1. Zip
      2. Combine latest
      3. Concatenating streams
        1. Concat
        2. Merge
    4. Filtering
      1. Cleaning stock adapter
        1. Advanced filtering with distinct calls
          1. Example of GroupBy
      2. Filtering tweets
    5. Summary
  11. Higher Level Abstractions
    1. Extracting code into methods
      1. Making conditions explicit
      2. Extracting consumers
        1. Using method references
      3. Extracting FlatMap
      4. Creating Factory Methods
        1. Financial stock quote Observable
        2. Tweet retrieval Observable
        3. Offline flow Observable
        4. Resulting flow
    2. Using Transformations
      1. Regular code extractions
        1. A Place where things start getting ugly
      2. Simplifying code with Transformations
        1. Extracting item persistence code
      3. Creating transformer classes
      4. File-based caching example
      5. Using Transformation to track execution time
      6. Using Transformation to debug Observables
    3. Summary
  12. Basics of Subjects and Preference Persistence
    1. Subjects
      1. PublishSubject
        1. Multiple sources
      2. BehaviorSubject
      3. ReplaySubject
      4. AsyncSubject
    2. Using subjects in the application
      1. Using RxPreferences
        1. Setup
        2. Examples with RxPreferences
      2. Subjects for Settings
        1. Connecting subjects to Settings
      3. Creating UI for Settings
        1. Creating Settings activity
        2. Preferences XML
        3. Options menu
      4. Updating flow
        1. Settings for financial stock updates
        2. Settings for monitored tweets
        3. Entire merge block
        4. Fixes for duplicate entries
    3. Summary
  13. Learning to Read Marble Diagrams
    1. Core elements of marble diagrams
      1. Elements of the diagram
      2. RxMarbles tool
    2. Examining operators
      1. Map
      2. FlatMap
      3. onExceptionResumeNext()
      4. GroupBy
      5. SubscribeOn and ObserveOn
      6. BehaviorSubject
    3. Summary