Moon Light Box

Time is Money

Android - Store Object in Shared Preferences

| Comments

1. Creating a Shared Preference

SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);

2. To Save

Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(MyObject);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();

3. To Retreive

Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
MyObject obj = gson.fromJson(json, MyObject.class);

Reference:http://stackoverflow.com/questions/7145606/

Comments

comments powered by Disqus