Android MVP – Organize your code

What is MVP?

After checking RxJava and Dagger2 it is now time to check one of the cool kids on block called MVP. It means Model – View – Presenter and it is an architectural pattern which is mostly used in the UI part of Android. If you go and search MVP in Google you will find many theoretical explanations. Let me try to make a practical one.



public class Author {
    private String name, country, link;

    public Author(String name, String country, String link) { = name; = link; = country;

    public String getName() {
        return name;

    public String getLink() {
        return link;

    public String getCountry() {
        return country;

This is a very simple model. Nothing but a class containing some data


In most of the cases you declare an interface with methods that you view will have. The presenter itself contains a reference to the view and calls these methods. The view also has a reference to a presenter. So, for example you have BooksActivity and you want to load books when the BooksActivity starts and a book can be clicked. Check the communication now:

View -> Presenter : Hey, onCreate was called. Can you please load some books.
Presenter -> View: Okay, I will call your presentBooks method and I will give you the data there
View -> Presenter: Hey, someone clicked a book, can you tell me what to do?
Presenter -> View: Show download dialog, I will tell you when to hide by calling your method.

Simple enough. Here is a sample view:

public interface BaseView {
    void hideLoading();
    void showLoading();

public interface AuthorsView extends BaseView {
    void presentAuthors(List authors);

public class BooksFragment extends BaseFragment implements BooksView {
    BooksPresenter booksPresenter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_books, container, false);
        ButterKnife.bind(this, view);

        // !!!!
        rvBooks.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;

    public void loadBooks(List books) {
        if(books.size() == 0) {
        } else {

        rvBooks.setAdapter(new BooksAdapter(getActivity(), books));

    public void hideLoading() {

    public void showLoading() {

It is just a simple UI component like a fragment or activity.


He accepts a view and calls it’s method when he decides it is appropriate. He takes care to check if the view is not destroyed and still alive. Also the component is responsible for making the appropriate services to get the data from the DB or network. Here is a sample:

public interface BooksPresenter  {
    void searchBooks(String q);
    void setView(BooksView view);

public class BooksPresenterImpl extends BasePresenter implements BooksPresenter {
    private ChitankaParser webParser;
    private WeakReference view;

    public BooksPresenterImpl(ChitankaParser webParser) {
        this.webParser = webParser;

    public void searchBooks(String q) {
        webParser.searchBooks(q).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe((books) -> {
            if (view == null || view.get() == null)
        }, (error) -> {
            if (view == null || view.get() == null)
            Timber.e(error, "Error receiving books!");
            view.get().loadBooks(new ArrayList<>());

    public void setView(BooksView view) {
        this.view = new WeakReference<>(view);

Did you get the idea? If not – check the links below to get a better sense:



You may also like...