android - Store Objects with GSON in Sharedpreferences. What do you think? -


after having hard time application object have (new?) idea exchanging data between activities using gson libary , sharedpreferences. has big advantage objects not destroyed if app shut down.

i want know thinks in terms of

  1. security
  2. performence
  3. app stability

here do. first create myexchanger class

 public class myexchanger {     //store variables here      string name;      list<vokabel> vokabeliste;   //put getters und setters here (or make them public)  } 

and helper class magic happens

public class exchangerutils {          context context;         sharedpreferences prefs;         sharedpreferences.editor editor;         resources res;         gson gson;         type type ;           public exchangerutils(context context) {                  this.context = context;                 prefs = preferencemanager.getdefaultsharedpreferences(context                                 .getapplicationcontext());                 res = context.getresources();                 type = new typetoken<myexchanger>() {                 }.gettype();         }          public void updateexchanger(myexchanger exchanger) {                  gson = new gson();                 string exchangerjson = gson.tojson(exchanger, type);                 editor = prefs.edit();                 editor.putstring(res.getstring(r.string.key_exchanger), exchangerjson);                  editor.commit();          }          public exchanger getexchanger(){                  string exchangerjson = prefs.getstring(res.getstring(r.string.key_exchanger), null);                 gson gson = new gson();                 if(exchangerjson!=null){                exchanger exchanger = gson.fromjson(exchangerjson, type);                  return exchanger;                 }                  return new myexchanger();         }  } 

the implementation someting likes

//somewhere in activity  exchangerutils utils = new exchangerutils(this); myexchanger exchanger = utils.getexchanger();  exchanger.setname("a name"); utils.update(exchanger); 

i have tested in 2 of apps , works fine, older phones , lot of objects (>100).

the annyong thing in implementation have update object. idea how without method?

i have suggestions improvement, feel free post it.

i created library persists data quite in same way describe: droidprefs

you can save , load values quite easily, don't have worry helper classes typetokens , that.

from pojo

public class animal {     private string mname;     private string mbreed;     private int mlegs;      public animal() {         mname = "";         mbreed = "";         mlegs = 0;     }      public animal(string name, string breed, int legs)     {         mname = name;         mbreed = breed;         mlegs = legs;     } } 

save object

animal newanimal = new animal("gitty", "octocat", 8); droidprefs.instance(context).put("animal", newanimal).apply(); 

load object

animal newanimal = droidprefs.instance(context).get("animal", animal.class); 

if object not serialised, new object created default constructor.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -