Android Sharedpreferences and Array

I created a weather widget. I save its configuration in SharedPreferences. The widget is updated by the service. I store weather information along with forecasts in an array. After the phone was turned off at night, I found that the array values ​​disappeared, maybe the system was suspended? service. Is there a way to keep the array in SharePreferencesso that it is more sticky.

+2
source share
1 answer

Of course, either come up with a way to serialize / deserialize your object from String, which you can store in a SharedPreference (if it is a simple array of numbers, for example, just a quick split / comma can do this basic JSON Serialization / Deserialization can work for slightly more complex data ) or use standard Java serialization to turn your object into a byte stream and save the data to a file , which you can read if necessary. However, if you really work with a lot of structured data, seriously consider using the excellent native SQLite support for Android, although this is a bit more.

+4

All Articles