EffectiveAndroid icon indicating copy to clipboard operation
EffectiveAndroid copied to clipboard

Presenter State Never Is Saved

Open erikjhordan-rey opened this issue 8 years ago • 3 comments

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!

erikjhordan-rey avatar Aug 20 '16 04:08 erikjhordan-rey

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 avatar Sep 28 '16 09:09 epelde

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.

epelde avatar Sep 28 '16 09:09 epelde

@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!

erikjhordan-rey avatar Sep 28 '16 17:09 erikjhordan-rey