java - Best place to store custom objects so all activities can see them -


i ios developer trying learn android , make sure following best practices.

i have custom objects need accessible 1 -> m activities , need saved when application closes. using sharedpreferences, code below, save them not sure if best route. should using singleton? there better way?

sharedpreferences  mprefs = getpreferences(mode_private); editor prefseditor = mprefs.edit(); gson gson = new gson(); string json = gson.tojson(userprofile); prefseditor.putstring("userprofile", json); prefseditor.commit();  gson = new gson(); json = mprefs.getstring("userprofile", ""); userprofileobject outobject = gson.fromjson(json, userprofileobject.class); 

a singleton won't saved when application exits. options are:

*sharedpreferences. small number of key/value pairs

*database. relational data

*file on disk, in whatever format prefer. amount of data, may need write custom parser.

storing json in shared preference bit weird. not horrible long aren't storing lot of keys in there, makes seem didn't know how write file.


Comments

Popular posts from this blog

many to many - Django Rest Framework ManyToMany filter multiple values -

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

Java Entity Manager - JSON reader was expecting a value but found 'db' -