Question

I created a weather widget. I store its configuration in SharedPreferences. The widget is updated by a service. I keep the weather information together with forecasts in an array. After the phone is off for the night i find that the array values are gone, maybe the system suspended? the service. Is there a way to store the array in SharePreferences so it is more sticky.

Was it helpful?

Solution

Sure - either come up with a way to serialize/deserialize your object from a String that you can store in a SharedPreference (if it's a simple array of numbers, for instance, just making a quick split/join on commas might do it; basic JSON serialization/deserialization might work for slightly more complex data), or use standard Java serialization to turn your object into a bytestream and save the data to a File that you can read when needed. If you're really working with a lot of structured data, though, seriously consider using Android's excellent built-in support for SQLite, although that's a little more work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top