Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP Android - Where to save the view state?

Tags:

android

mvp

I'm with some doubts regarding the state save using MVP on Android. I defined my fragments/activities as views and then implemented the corresponding presenters.

My example is simple:

I have a activity with some checkboxes and spinners. If the activity is destroyed by the Android system and then recreated, where should I save these spinners and checkboxes states? On the view? On the presenter?

If on the view, should I have the restore logic on the view or on the presenter?

Thank you!

like image 869
Fábio Carballo Avatar asked May 19 '15 17:05

Fábio Carballo


People also ask

What is saving state in Android?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.

What is presenter in MVP Android?

Example of MVP Architecture The role of the Presenter class is to keep the business logic of the application away from the activity. Below is the complete step-by-step implementation of this android application. Note that we are going to implement the project using both Java and Kotlin language.


1 Answers

In the case of MVP, is the model who is in charge of keeping the state of the view.

For example, in you model you have a Post entity with an Array of Categories. In your view you have a Checkbox for each category, and in each checked/unchecked action you add/remove objects from the Post's Category array.

Once the Activity is restored, the View should ask for the Post's Category array in order to find out which are selected and which aren't, so it can set the proper checked/unchecked attribute.

Here's a really good post about it: http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/

like image 173
mmark Avatar answered Sep 19 '22 13:09

mmark