What's the best way to save the data stored in the Application Class (singleton) of an Android Application?
I have a quiet big app that shares a lot data between the activities. So most of it is stored on the Application Singleton.
It all works great.. util the application is killed by the OS on low memory... then when it comes back it tries to resume the activity without success due to the lack of necessary data that was before on Application.
Due to the lack of a much appreciated (and needed) method to save data on Application according to your experience what are the best approaches?
Can i save stuff, besides the "normal" strings, booleans, etc, like Bitmaps?
I have already seen this How to declare global variables in Android? but the question isn't focusing on what is important in this case, how to save the data when the application is killed due to low memory...
As with many questions, there is no simple answer. There are many ways to save data and each has advantages and disadvantages. The "best" approach will depend on your particular needs. You have all your options here: http://developer.android.com/guide/topics/data/data-storage.html
SharedPreferences stores strings, so anything that is a string can be stored, including any serialized/encoded object. According to this post, there is no hardcoded size limit for a serialized string in SharedPreferences, but is based on the String size limit. Nevertheless, this other post points out that the whole SharedPreferences object is written as a single xml file, so you should try to keep its size to a minimum.
JSON object (or using GSON as suggested by katit) are a good lightweight option, but the approach I would take is to save them to the internal data storage (unless the data is really big, i.e., many megabytes, and you prefer the external storage) and keep the links only in the SharedPreferences. I don't know what your objects look like, but if they can be reduced to a bunch of simpler components, you can consider a database for them (i.e., one row per object, one column per field, including perhaps a few blobs).
The files vs database approach would depend also on how many times are you planning to access those objects. If they will be read one or two times only and then disappear, then I would choose files over the hassle of the database and its cursors. I would choose a db if there will be many reads, and perhaps you need a faster search using queries.
Check also this post: http://android-developers.blogspot.in/2009/02/faster-screen-orientation-change.html for an Activity-specific option.
It's important to note that if you are using a singleton class to hold your information and your application is forced to stop, the information will be cleared.
For shared preferences the information will remain the same.
Hope this helps.
There is Java serializer, not sure is that what you need.
I personally use GSON for all of that. It's google library to work with JSON. It allows to serialize objects into efficient string representation.
I used this mainly for RESTful service communication but then learned that it works very good to store object representation to SQLLite or whatever. I can inflate object very easy this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With