Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android MVP pattern and multi presenter Activities

Android MVP pattern and multi presenter Activities

This presentation describes the MVP architectural pattern on Android and its main theory of the separation of concerns. Also presents a variation implemented and used by the Android team of Taxibeat, regarding the implementation of multiple Presenters and Views in a single Android Activity.

Chryssa Aliferi

October 13, 2016
Tweet

More Decks by Chryssa Aliferi

Other Decks in Programming

Transcript

  1. Our beloved Android: Everything is connected with everything. UI +

    Business Logic + Data + Multiple Threads = Android GOD Activity
  2. What is MVP? Model: is a data layer such as

    database or a set of classes that provide the data we want to display in the view. View: represents the UI components and transforms the information contained by the model into the UI. Presenter: is a layer that provides View with data from Model, according to some rules.
  3. MVP Model Model: • Set of classes that define the

    data. • Business rules for data and data transformations • Data Retrieval
  4. View: • Activity, Fragment, View, Dialog • Reference to the

    Presenter • Exposes methods and Events to the presenter • Transforms the information contained by the model into the UI. • Animations • Navigating to other screens MVP View
  5. Presenter: • Middle man between View and Model • Controls

    and updates UI • Handles all UI events on behalf of the view • Retrieves data from the Model layer and displays them in the View MVP Presenter
  6. • View interface - “Screen” • all “Screen” methods refer

    to view behaviour • Android Activity • “screen” methods implemented • Presenter class & Presenter “children” flows • accesses view through screen MVP Implementation
  7. MVP Example A new screen concerning the Payment feature, with

    two modes:
 • Select Payment • Edit Payment
  8. Taxibeat LocateActivity.java: • LocateActivity.java is the Main Taxibeat Activity •

    LocateActivity.java contains 2 basic Presenters, the LocatePresenter.java and the MenuPresenter.java • LocatePresenter.java has 3 presenter flow descendants, the PickupFlow.java, the DropOffFlow.java and the CallingFlow.java
  9. PickupFlow.java • LocatePresenter.java descendant • PickupFlow.java presenter contains all the

    logic behind the process of setting the correct pickup location. • onAddressAnimationEnd(), PickupFlow calls “destroys” itself and gives control to DropOffFlow. • onBackButton() press we terminate the app.
  10. DropOffFlow.java • LocatePresenter.java descendant • DropOffFlow.java presenter contains all the

    logic behind the process of setting the dropoff location and all the hailing options. • onClick() of the green button, DropOffFlow “destroys” itself and gives control to CallingFlow. • onBackButton() press we handle back control to the PickupFlow.
  11. CallingFlow.java • LocatePresenter.java descendant • CallingFlow presenter contains all the

    logic behind the process of calling a taxi. • onClick() of the red button or on onBackButton() press, CallingFlow “destroys” itself • Then gives control back to DropOffFlow.
  12. MenuPresenter.java • MenuPresenter presenter contains all the logic behind the

    menu initialisation and menu onClick actions. • MenuPresenter is an autonomous presenter that handles only menu actions.
  13. MVP Pros vs Cons Pros: • Separation of concerns •

    Maintainability • Re-usability of views, presenters and screens • Less time spending on view control • Information hiding • Testable! Cons: • Redundancy • Learning curve • Takes time, not suitable for small projects.
  14. MVP Do not forget: • Use your logic, to distinguish

    the main business flows • Create your “Screen”, your View interface • Activities, Fragments or custom views implement the View interface • Separate your app business logic by moving it to a plain java Presenter object • Make Presenter “children” if needed to make your code even cleaner • Spend time writing a clean interface between your Presenter and your View • Test your Presenter. • Retain the state of the Presenters, “initialise” and “destroy” them.
  15. Resources: • Is Activity GOD? by Kenju Wagatsuma on SlideShare

    • MVP design pattern for Android Apps by Infinum on SlideShare • Android Code That Scales, With MVP by Nathan Barraille • MVP for Android: how to organize the presentation layer by Antonio Leiva • Introduction to Model View Presenter on Android by Konstantin Mikheev