I have an ArrayList
with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don't need the array available after the application has been closed completely. I save a lot of other objects this way by using the SharedPreferences
but I can't figure out how to save my entire array this way. Is this possible? Maybe SharedPreferences
isn't the way to go about this? Is there a simpler method?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
My utils class for save list to
SharedPreferences
Using
.
Full code of my utils // check using example in Activity code
Saving
Array
inSharedPreferences
:Loading
Array
Data fromSharedPreferences
You can also convert the arraylist into a String and save that in preference
You can save the Arraylist after converting it to string using
convertToString
method and retrieve the string and convert it to array usingconvertToArray
After API 11 you can save set directly to SharedPreferences though !!! :)
don't forget to implement Serializable:
https://stackoverflow.com/a/7635154/4639974
best way is that convert to JSOn string using GSON and save this string to SharedPreference. I also use this way to cache responses.
You can convert it to a
Map
Object to store it, then change the values back to an ArrayList when you retrieve theSharedPreferences
.