Robert Simoes Robert Simoes - 4 months ago 15x
Android Question

How does GSON + Shared Preferences work in the context of memory for Android?

This might be more of a pure CS question, but I am having a trouble understanding how

works in the context of saving and resuming an application when saving Objects in

When you save GSON to Shared Preferences, is the GSON Strings themselves the only things that are saved (ie. the objects are lost)?

And if this is true, when you resume the application, how are the objects retrieved ?

Are they re-instantiated with the GSON data or does the GSON data store a location in memory within itself, or is the Shared Preferences the overall location stored in memory?

For example,


class TaskManager {
String managerName;
LinkedList<Task> taskList = new LinkedList<Task>();

class Task {
String taskName;
int Time;

class FooBarActivity extends Activity {
TaskManager manager = new TaskManager("manager1")
Task newTask = new Task("task1");


SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(manager);
prefsEditor.putString("manager", json);

manager object JSON:


Is that task1 element of the List:

  • contain all of the instance variables of the original Task object (ie. it's name/time?)

  • OR Store a location in memory (say @40E1, idk)

  • OR is it just a plain string which I have to create a new object and use it as data to instantiate?


You're making a big confusion of very distinction stuff.

  • JSON it's a generic data format represented as human-readable string.
  • GSON it's a library developed to serialize and deserialize JSON strings into Java objects. Anything from that object that it's not data or metadata won't be saved. For example, if the object have a Context, or a Thread. It can only save stuff that can be represented as simple strings. For example Strings, int, float. Please see here about serialization What is object serialization?

note that JSON and GSON have very little to do with Android. One is a data representation format and the other is a Java library.

  • SharedPreferences it's a feature of the Android Framework that can save/load some basic types of data (String, int, float, Serializable, etc) in a key/value pair.

Now that we have some basic concepts laid out, let's check your questions:

prefsEditor.putString("manager", json); this line is only getting a string and saving it into the SharedPreferences.Editor. Nothing more.

at some point you'll have to code String json = prefs.getString("manager", null); and the only thing this line is to get a String from the SharedPrefenreces (if it exists).

and later on when you code TaskManager manager = gson.fromJson(json, TaskManager.class);, this is creating a NEW TaskManager object that have the same managerName and that the taskList contains Tasks objects with the same taskName and time as the previous tasks objects you had before. But those are all new objects.

I hope it's more clear.