0

How do i save the state of all the fields i have in my class that extends View?

@Override
protected Parcelable onSaveInstanceState() {
    Parcelable localState = super.onSaveInstanceState();

    Bundle bundle = new Bundle();
    bundle.putSerializable("gameEngine", this.gameEngine);
    bundle.putParcelable("inherited", localState);
    return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable localState) {
    Bundle bundle = (Bundle) localState;
    this.gameEngine = ((GameEngine) bundle.getSerializable("gameEngine"));
    super.onRestoreInstanceState(bundle.getParcelable("inherited"));
}

This doesn't seem to save the fields.

Thanks.

Waddas
  • 1,353
  • 2
  • 16
  • 28

2 Answers2

0

It may not be the best solution to do this inside a single view object. It would be much easier to save the state of your view from inside the fragment or an activity containing the view. Both Fragments and Activities offer methods to retain state instance following the pattern you used in your sample code.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
0

Put all your values in an array and restore them manually.
Or store them in a custom class.

Or if you have only a few values put them one by one in the Bundle:

bundle.putString("1", value);
bundle.putString("2", value);
etc.
Remc4
  • 1,044
  • 2
  • 12
  • 24
  • This answer was correct, however for people that need to store a lot of variables this may not be the best solution. Also i found you cannot put a multidimensional array in the bundle. I had to create an algorithm to flatten the multi into a single for storing and then another to reconstitute it back into a multi for restoring. Thanks :) – Waddas May 02 '13 at 00:25
  • As far as I know this is the only way to do it. You can use putSerializable for multidimensional arrays. Note that everything that goes through Bundle is serialized (cloned), so all references to other activities are lost. You can use static variables to keep them. And sorry for the late answer, I'm from Europe. – Remc4 May 02 '13 at 07:48