Master MVP with RxJava: Build Decoupled Android Apps Quickly
This article explains how to combine MVP architecture with RxJava in Android development, covering framework selection, step‑by‑step MVP construction, thread management using RxJava, and practical Q&A on testing, modularization, and memory‑leak prevention, providing a comprehensive guide for building decoupled, responsive mobile apps.
Dev Club is a community for mobile developers to share technology, make friends, and expand networks. Members are vetted Android engineers. Weekly guest talks and discussions are held.
In this session, Tencent IEG Android engineer Dai Jun presents "An MVP Implementation Based on RxJava".
Overview: RxJava enables reactive programming in Java, organizing asynchronous events as sequences. MVP separates View/Business logic from Model, facilitating decoupling, thread control, unit testing, and more.
1. Choosing an Android Development Framework
Native Android follows a basic MVC pattern, but Activities often become bloated, handling both View and Controller responsibilities. Early projects saw Activities reaching 2000‑3000 lines.
Two main solutions:
Layered architecture
Modularization
Both aim to achieve decoupling; layering addresses vertical separation, while modularization handles horizontal separation.
MVP is a layered approach that achieves decoupling.
2. Step‑by‑Step MVP Layer Construction
Reference articles include Android Application Architecture, Android Architecture Blueprints, Google’s MVP example, and various GitHub repositories.
Below is a typical MVP diagram showing added Presenter and DataManager layers.
Example implementation:
Assume a simple feature that fetches a string from a server and displays it.
Activity contains:
MainView interface (View layer)
Presenter (P layer)
The View interface defines onShowString(). The Presenter holds a reference to a Model interface, isolating business logic from UI.
Using interfaces allows swapping implementations (e.g., real DataSourceImpl vs. test DataSourceTestImpl), enabling fake data for UI development or testing without backend readiness.
When backend APIs are unavailable, developers can use the test implementation to mock data, facilitating UI work and reducing dependency on external services.
This separation also simplifies unit testing and prevents dirty data from reaching production.
However, Presenter‑initiated network calls on the main thread cause NetworkOnMainThreadException. Traditional solutions involve creating new threads and posting results back to the UI thread, which can become messy.
3. Using RxJava for Thread Control
RxJava provides concise asynchronous handling compared to AsyncTask or Handlers.
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'In the Presenter, getData() is refactored so that data logic runs on Schedulers.io() and UI updates on AndroidSchedulers.mainThread().
RxJava also supports caching, concurrent requests, dependent API calls, click‑debounce, reactive UI, and complex data transformations.
Common RxJava scenarios include:
Checking cache before fetching data
Waiting for multiple concurrent requests before updating UI
Chaining dependent API calls
Preventing rapid button clicks
Building reactive interfaces
Performing complex data transformations
Further reading: "RxJava for Android Developers", "RxJava + Retrofit Best Practices", "RxJava Use Cases", and "How To Use RxJava".
Conclusion
Combining MVP with RxJava yields a more flexible Android architecture with these benefits:
Each layer communicates via interfaces, ensuring independence.
Separate implementations for production and testing simplify mock data usage.
All business logic runs off the UI thread, minimizing UI blocking.
RxJava’s chainable operators eliminate nested callbacks.
The session concluded with a Q&A covering package structuring, memory‑leak handling, presenter‑view binding, testing strategies, and advanced RxJava usage.
Tencent TDS Service
TDS Service offers client and web front‑end developers and operators an intelligent low‑code platform, cross‑platform development framework, universal release platform, runtime container engine, monitoring and analysis platform, and a security‑privacy compliance suite.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
