rallat / EffectiveAndroid

This sample project shows how to apply MVP and Clean architecture on an Android app
1.7k stars 166 forks source link

Presenter State Never Is Saved #19

Open erikjhordan-rey opened 8 years ago

erikjhordan-rey commented 8 years ago

I've been reviewing your project seems good but I've been trying and never saves the state of the presenter.

When you save the state using PresenterHolder.getInstance () putPresenter (TopImagesListActivity.class, presenter); works well but I found that when I try to recover the state the task of being saved even then not complete when I get the instance again PresenterHolder.getInstance().getPresenter (TopImagesListPresenter.class); always is null.

Your example

if (presenter != null) {
//This line never is executed
            presenter.setView(this);
        } else {
            presenter = new TopImagesListPresenterImpl(this);
//always is executed
        }
}

Do you have any other alternative that you used?

Thank you for your contribution and your awesome job!

epelde commented 8 years ago

As @epeterson320 has advised (pull request), there is a bug. The key (TopImagesListActivity.class) used to store the presenter instance and the key used to retrieve (TopImagesListPresenter.class) are different, so you'll always get null when trying to get the instance.

epelde commented 8 years ago

I have a question. Is it a good idea to use the Class as the key to store/retrieve presenter instances? How do we store many instances of the same presenter? Suppose we need multiple instances to display similar data using a ViewPager.

erikjhordan-rey commented 8 years ago

@epelde yes of course! @epeterson320 is right the key class of HashMap were different, we were trying to get TopArticleListPresenter instance but the right key to retrieve the instance is TopArticleListActivity.class not TopArticleListPresenter.class. although in solving @epeterson320 did the other way around and of course also works.

I think that the correct solution should be:

Issue

TopArticleListPresenter presenter = PresenterHolder.getInstance().getPresenter
                (TopArticleListPresenter.class);

Solution

TopArticleListPresenter presenter = PresenterHolder.getInstance().getPresenter
                (TopArticleListActivity.class);

You can see TopArticleListActivity.class here.

cheers!