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?
This method is used to store/save array list:-
This method is used to retrieve array list:-
With Kotlin, for simple arrays and lists, you can do something like:
and then access the preference easily:
You can save String and custom array list using Gson library.
=>First you need to create function to save array list to SharedPreferences.
=> You need to create function to get array list from SharedPreferences.
=> How to call save and retrieve array list function.
=> Don't forgot to add gson library in you app level build.gradle.
implementation 'com.google.code.gson:gson:2.8.2'
All of the above answers are correct. :) I myself used one of these for my situation. However when I read the question I found that the OP is actually talking about a different scenario than the title of this post, if I didn't get it wrong.
"I need the array to stick around even if the user leaves the activity and then wants to come back at a later time"
He actually wants the data to be stored till the app is open, irrespective of user changing screens within the application.
"however I don't need the array available after the application has been closed completely"
But once the application is closed data should not be preserved.Hence I feel using
SharedPreferences
is not the optimal way for this.What one can do for this requirement is create a class which extends
Application
class.Using the setter and getter the ArrayList can be accessed from anywhere withing the Application. And the best part is once the app is closed, we do not have to worry about the data being stored. :)
After API 11 the
SharedPreferences Editor
acceptSets
. You could convert your List into aHashSet
or something similar and store it like that. When your read it back, convert it into anArrayList
, sort it if needed and you're good to go.You can also serialize your
ArrayList
and then save/read it to/fromSharedPreferences
. Below is the solution:EDIT:
Ok, below is the solution to save
ArrayList
as serialized object toSharedPreferences
and then read it from SharedPreferences.Because API supports only storing and retrieving of strings to/from SharedPreferences (after API 11, its simpler), we have to serialize and de-serialize the ArrayList object which has the list of tasks into string.
In the
addTask()
method of the TaskManagerApplication class, we have to get the instance of the shared preference and then store the serialized ArrayList using theputString()
method:Similarly we have to retrieve the list of tasks from the preference in the
onCreate()
method:You can get
ObjectSerializer
class from Apache Pig project ObjectSerializer.java