Skip to content Skip to sidebar Skip to footer

Store Objects In Android

I would like to know what would be the best option to store a custom Java object that I have in my application. I've read about serialization but it seems to be quite slow. What is

Solution 1:

If you're not storing an over abundance of data you can use Gson to convert a Java object to a Json string and store the string in your app preferences. Gson has a toJson(obj) method and visa-vis.

Getting data out of app preferences and into an object:

String json = appSharedPrefs.getString("someName","");    
return gson.fromJson(json, YourModel.class);

To get data from your object into app preferences:

Stringjson= gson.toJson(obj);

Then prefsEditor.putString("someName", json).apply().

Solution 2:

Post a Comment for "Store Objects In Android"