0

My Android application has an array of fairly-complex objects that I use to store user data and I would like to save this array and restore the data from previous sessions when running the application. The objects in question have several pieces of data (hence making the objects in the 1st place), so I really don't want to write out all the components to SharedPreferences (which is one idea I had for doing this). I also looked into onSaveInstanceState(), that doesn't appear to be called reliably enough to help me.

The object in question is Serializable, but it doesn't appear that there's a putSerializable() method I can use, so I can't see how this helps me with this issue.

Can anyone provide a suggest for dumping this data somewhere non-volatile and re-using later??

Rich
  • 4,157
  • 5
  • 33
  • 45

3 Answers3

1

Using SharedPreferences is not ideal for anything beyond fairly simple key-value pairs. Your best bet would be to use a SQLite Database. Check the developer docs for more info.

Kent Hawkings
  • 2,793
  • 2
  • 25
  • 30
  • My application does have a database already, so at least that step is already accomplished, but I'd still have to write out all the primitives in my object, one by one, wouldn't I? – Rich Nov 12 '15 at 18:39
1

If you're set on just rolling with Serializable, you could consider the same sort of approaches used in plain Java-land for writing a serializable to a String to store in a SharedPreference; see this question for one approach.

Otherwise, I think you're better off using something like GSON, like Zanna suggeted.

Community
  • 1
  • 1
Yoni Samlan
  • 37,905
  • 5
  • 60
  • 62
  • While it turns out to be mostly simple to convert a serializable object (more specifically, an ArrayList of serializable objects) to a string, I'm still a bit surprised the code is as complicated as it is. Anyway, your example was very helpful, especially since I remain unmotivated to figure out how to roll Gson in to my Android project. – Rich Nov 12 '15 at 20:28
  • Yeah, OOB Android persistence story is very manual. GSON's not so scary, though - if you have exposed properties or getters/setters plus a no-args constructor, you can basically just call `GSON.toJson(myInstance)` and `GSON.fromJson(someJsonString, MyClass.class)` to what you need. The bonus is debugging will be a lot easier, since the data you're persisting is normal human-readable JSON, and upgrading from one model version to another (for example, you add a new field to a model while you still have a stored copy from the old representation in the DB) is less likely to break the universe. – Yoni Samlan Nov 16 '15 at 19:52
0

You could use yours application's internal storage to save those data and restore them every time you need through a FileInputStream.

If your object is complex, in order to save space, you could serialize it with Gson and then save it as a simple string, always in the internal storage.

Example:

Object yourObject = new Object;
Gson gson = new Gson();
String serializedObject = gson.toJson(yourObject);

//write to internal storage
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

You could read that string in this way:

String filename = "your_file";
FileInputStream fis = openFileInput(filename);
StringBuffer sBuffer = new StringBuffer();
DataInputStream dataIO = new DataInputStream(fis);
String strLine = null;
while((strLine = dataIO.readLine()) != null)
{
sBuffer.append(strLine+”\n”);
}
dataIO.close();
fis.close();

and finally deserialize it.

Marco
  • 705
  • 8
  • 28