aquaflamingo
aquaflamingo

Reputation: 822

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 SHARED PREFERENCES works in the context of saving and resuming an application when saving Objects in GSON Format.

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,

Context

TaskManager.java

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

Task.java

class Task {
    String taskName; 
    int Time;
    }

FooBarActivity.java

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

    manager.taskList.add(newTask);

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

manager object JSON:

{"manager":[
    {"name":"manager1"
     "taskList":"[task1]"}
]}

Is that task1 element of the List:

Upvotes: 0

Views: 723

Answers (1)

Budius
Budius

Reputation: 39846

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.

Upvotes: 1

Related Questions