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
Post a Comment