Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save an instance of a custom class in onSaveInstanceState?

Tags:

android

I created an instance of a custom class RestaurantList to hold my data (a list of restaurant data received from a web service as json data).

How can I save it in onSaveInstanceState?

like image 608
jul Avatar asked Jul 03 '10 17:07

jul


People also ask

What is Save instance?

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.

Which method is be called to allow the activity to save per instance state?

As your activity begins to stop, the system calls the onSaveInstanceState() method so your activity can save state information to an instance state bundle.

What is the purpose of using onSaveInstanceState () here?

Basically, onSaveInstanceState is used for the scenario where Android kills off your activity to reclaim memory. In that scenario, the OS will keep a record of your activity's presence, in case the user returns to it, and it will then pass the Bundle from onSaveInstanceState to your onCreate method.


2 Answers

Custom objects can be saved inside a Bundle when they implement the interface Parcelable. Then they can be saved via:

    @Override     public void onSaveInstanceState(Bundle outState) {         super.onSaveInstanceState(outState);         outState.putParcelable("key", myObject);     } 

Basically the following methods must be implemented in the class file:

 public class MyParcelable implements Parcelable {      private int mData;       public int describeContents() {          return 0;      }       /** save object in parcel */      public void writeToParcel(Parcel out, int flags) {          out.writeInt(mData);      }       public static final Parcelable.Creator<MyParcelable> CREATOR              = new Parcelable.Creator<MyParcelable>() {          public MyParcelable createFromParcel(Parcel in) {              return new MyParcelable(in);          }           public MyParcelable[] newArray(int size) {              return new MyParcelable[size];          }      };       /** recreate object from parcel */      private MyParcelable(Parcel in) {          mData = in.readInt();      }  } 
like image 173
Maaalte Avatar answered Sep 30 '22 02:09

Maaalte


I know "that this case is cold", but because i found this thread first, when I was searching for exactly the same thing (and found an answer by now):

Imagine Bundle as an XML file. If you create a new <BUNDLE name="InstanceName" type="ClassName"> you can freely add elements and attributes in a fresh and empty namespace.

When onSaveInstance(Bundle outState) of your MainActivity is called (you can also force this in onPause), you can create a new: Bundle b = new Bundle();

Then call your (probably not inherited and not overriden) custom Method onSaveInstance(Bundle b) in your own class with your newly created Bundle b. Then (in onSaveInstance(Bundle outState)) of your MainActivity, call outState.putBundle("StringClassAndInstanceName", b);

When you find this string in onCreate, you can either use a switch/case to recreate this object or (better) have a factory function in your custom class to work with Bundle and "StringClassAndInstanceName".

like image 43
dsewtz Avatar answered Sep 30 '22 02:09

dsewtz