I have a view(Activity) with imageView and textView. When I click on imageView then in the textView show how many time I click on the image. I use pattern MVP to solve this task.
And as result, all work fine. OK. Nice.
But have one more task - to restore view state after rotate screen.
To solve this task I use the next approach: 1. Create Presenter as Singleton 2. I Presenter I have variable
private int countClick;
When clicking ImageView on the View I call
presenter.clickLike()
, increment it and call View methodshowCountOfClick()
In the Presenter
public void clickLike() { countClick++; view.showCountOfClick(countClick + ""); }
Also in Presenter I have method
@Override public void viewIsReady() { view.showCountOfClick(countClick + ""); }
This method call from View in onCreate()
method()
In the View set text in
TextView
with the count of clicks.public void showCountOfClick(String text) { textView.setText(text) }
Done.
So as result the count of click shows correct when rotating screen.
And a bonus I do not need in View to override method protected void onSaveInstanceState(Bundle outState)
because all work is done in Presenter.
Question: Is it a good solution?