Holding data model in android application

Issue

Currently I’m writing android application, whose main functions are just fingerpainting on screen. The main problem is how to store application’s data model. What would be the best practice?
Currently data model looks like this:

public class BoardModel {
public Bitmap mBoard;

public int mLineWidth = 1;
public int mForegroundColor = Color.GREEN;
public int mBackgroundColor = Color.BLACK;
}

This class is instantiated when onCreate method of main activity is called. During it’s lifecycle activity can be destroyed. For example it happens when user changes the device orientation from portrait to landscape. So to store my model I’m overriding onSaveInstanceState like this:

protected void onSaveInstanceState(Bundle outState) {
    outState.putParcelable(KEY_MODEL, mModel);
}

and modified onCreate method like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
        mModel = new BoardModelParcelable();
    } else {
        mModel = savedInstanceState.getParcelable(KEY_MODEL);
    }       
}

Also I added following class to implement Parcelable interface

public class BoardModelParcelable extends BoardModel implements Parcelable{
....
}

The problem is that when I add any data to my model I have to modify BoardModelParcelable to store it. For example I’m planning to add class that describes the instrument, that is used to draw something. In this case I need to create another class for instrument, that implements Parcelable interface? Or should I perform storing instrument data in BoardModelParcelable? Looks like I must implement too much parcelable objects. Any Ideas how to store my model data without creating saving code this much?

Solution

You can use Fragment without UI to persist data between activity re-creation. You must call setRetainInstance(true) to tell the framework to keep this fragment around during a configuration change. So after configuration change activity will be re-created but this fragment will stay in the previous state with all your data. Very good article on this topic: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

“Ever since the introduction of Fragments in Android 3.0, the recommended means of retaining active objects across Activity instances is to wrap and manage them inside of a retained “worker” Fragment. By default, Fragments are destroyed and recreated along with their parent Activitys when a configuration change occurs. Calling Fragment#setRetainInstance(true) allows us to bypass this destroy-and-recreate cycle, signaling the system to retain the current instance of the fragment when the activity is recreated.”

Answered By – makovkastar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published