Thursday, October 6, 2016

Save objects in Shared Preferences


private SharedPreferences preferences;
private SharedPreferences.Editor editor;

//Save objects
preferences = context.getSharedPreferences(namePreferences, mode);
editor = preferences.edit();
editor.putString(key, GSON.toJson(object));
editor.commit();

//Retrieve objects
String gson = preferences.getString(key, null);
Object o = GSON.fromJson(gson, a);

No comments: