Android with no Lifecycle: MPVsV approach with Moxy library

Alexander Blinov
Aug 7, 2016 · 4 min read

The significant part of Android community uses MVP approach to split application business scenario into logic layers. Unfortunately, adding MVP to Android is not an easy task because of Activity and Fragment Lifecycle. Furthermore, adding MVP causes a lot of boilerplates while saving state. Let us* introduce an open source library for implementing MVP into Android applications.

Wishlist

  1. Painlessly integrate MVP into an application
  2. A long asynchronous task may deliver results to activity after configuration changes (f.e. rotate) and Activity restart (press back and start again)
  3. Reduce Activity and Fragment saving state boilerplate (typical for MVP)
  4. Connect multiple views for presenter (not a canonical MVP feature, but a really useful one)

Model — Presenter — ViewState — View approach

Our application model works according to the following scheme:

Image for post
Image for post

The scheme description:

Image for post
Image for post

Moxy

The scheme above is implemented under Moxy. Here is a brief overview of the library’s major parts.

Every presenter is responsible for its own piece of logic. It should extend MvpPresenter<View extends MvpView>.

To pass in the View command you need to call a method:

public View getViewState() 

It stores command in ViewState queue and then dispatches it to the view. Note that one Presenter can be attached to several views! All of those views get this command.

After the first view is attached to the presenter, the presenter invokes a callback:

protected void onFirstViewAttach()

At this callback, you can init your presenter

The presenter has the @InjectViewState annotation for the ViewState binding. A parameterless annotation means that ViewState will be generated automatically.

Every View interface should extend MvpView. In View, you declare commands (methods) that will be called by a presenter. Commands may have @StateStrategyType annotation. It is used to generate ViewState.

To generate ViewState, View interface and @StateStrategyType annotation is used. ViewState implements this View.

StateStrategyType declares ViewState behaviour strategy after invoking its method. The strategy can change a command queue (f.e. Remove the same command from the queue or clean it). By default AddToEndStrategy is used that puts command to the end of the queue.

View Implementation (ViewImpl) should implement View. It also should be able to attach and detach presenters. By default we provide MvpFragment and MvpActivity that have a smart attach-detach mechanism. If you want to use it, you should add @InjectPresenter annotation to the Presenter field.

InjectPresenter annotation also manages Presenter lifetime.

PresenterType.LOCAL means your presenter will be stored only while config is changing.

PresenterType.GLOBAL allows to store presenter for as long as you want. To assess the global presenter you should declare tag parameter in the InjectPresenter annotation. It allows Moxy to map stored presenters and views.

Feel free to organize your model-presenter communication. You should only note that presenters work on the Main Thread. So you have to run a long operation on another thread.

I prefer to inject a model into a presenter with Dagger (good method for injecting a model into a presenter is onFirstViewAttach) and make communication using Rx.

To avoid unexpected behaviour you should follow the interaction order between the components as shown in the graph below.

Image for post
Image for post

Moxy is very flexible. That was just a quick overview of the basic functionality.

Of course, you can manually:

  1. Attach/detach presenters (just skip InjectPresenter annotation and use attach/detach methods)
  2. Create custom view state (just add your custom view state class to InjectViewState annotation)
  3. Manage Presenter’s instantiating (there is a PresenterFactory mechanism) and recycling (you have access to PresenterStore)
  4. Create custom StateStrategyType

Moxy is too complex to describe in a single article. In the upcoming articles I want to discuss:

  1. Android studio template to generate Presenter-View-ViewImpl classes and its cross connection
  2. How to use Moxy with stuff like Custom View and Adapters
  3. Generics in Moxy

Moxy is a flexible open source product. Fill free to comment, create issues and contribute to the Moxy repository. Moxy is available at Maven repository. Learn how to add Moxy to your project on Moxy Github page. You can also check samples at MoxySample Github repository.

Written by Moxy creators Alexander Blinov and Yuri Shmakov.

*

Redmadrobot Mobile

Building apps and sharing thoughts on mobile.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store