Reputation: 67
After having a hard time with the Application Object I have a (new?) idea for exchanging Data between Activities using the GSON Libary and Sharedpreferences. This has the big advantage that the objects are not destroyed if the app is shut down.
I want to know what you thinks in terms of
Here is what I do. First Create a MyExchanger class
public class MyExchanger {
//store your variables here
String name;
List<Vokabel> vokabeliste;
//put your GETTERS und SETTERS HERE (or make them public)
}
And then a helper class where the 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){
My Exchanger exchanger = gson.fromJson(exchangerjson, type);
return exchanger;
}
return new MyExchanger();
}
}
The implementation could look someting likes this
//Somewhere in the activity
ExchangerUtils utils = new ExchangerUtils(this);
MyExchanger exchanger = utils.getExchanger();
exchanger.setName("a name");
utils.update(exchanger);
I have tested it in two of my apps and it works just fine, also with older phones and a lot of objects (>100).
The only annyong thing in the implementation is that I have to update the object. Any idea how to do this without this method?
I have any suggestions for improvement, feel free to post it.
Upvotes: 1
Views: 2151
Reputation: 9223
I created a library that persists data quite easily in the same way that you describe: DroidPrefs
You can save and load values quite easily, but you don't have to worry about the helper classes with TypeTokens and all that.
From this 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 the object was not previously serialised, a new object will be created with the default constructor.
Upvotes: 4
Reputation: 8641
Hmmm. It's an interesting idea, but SharedPreferences isn't really the best thing for transferring and persisting data. Of course, as you've seen, neither is Application or any other sort of static variable.
I can't propose an exact alternative without knowing what you're trying to do, but in the same circumstances I'd use a database wrapped by a content provider. You may consider this too much work for what you're trying to do, but a database is persistent, indexable, searchable, and easily updated.
If you want to pass transient data between Activities, use Intents with objects in their extended data. If the data isn't really transient, persist it somewhere. If it's a small amount of data, such as settings, persist it in SharedPreferences. If it's a large amount of linear data, such as a JPEG image, persist it in a file. If it's structured data, persist it in a database. However, don't try to substitute one of these for another one just because it's "easier".
Upvotes: 1