I’m working on a simple app that needs to generate an instance of a class and keep getting fed with user input. After the app exits, i want this instance to be preserved. I tried storing the user input in a file and making a new instance of the file on consecutive runs using the stored data, but due to the complex nature of the class, the results aren’t as good as expected.
So, my question is, what is the best way to store an instance of a class on a devices internal memory, and loading it again each time the app is launched again? From what I understand, I’ll need to serialize the object, but I’m not sure how exactly to, or if that is the correct approach.
Thanks for any input.
You need to persist your data in the
onPause()method and load it again in theonResume()method.Typically, one uses the build-in SQLite database (http://developer.android.com/guide/topics/data/data-storage.html#db). You can also try JSON (http://developer.android.com/reference/org/json/JSONObject.html) to serialize your object and store it in Android’s
SharedPreferences(http://developer.android.com/guide/topics/data/data-storage.html#pref).