Most Android apps need to save data about the application state so that users progress is not lost. SharedPreferences API in android lets us save data in key-value sets. When there is a need to save a small collection of key-values we should use Android SharedPreferences.
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.
Step 1:
To use android shared preference we have to call method getSharedPreferences() that returns a SharedPreference instance poiting to the file that contains the values of preferences. The first parameter is the key and the second parameter is the MODE as shown below,
To use android shared preference we have to call method getSharedPreferences() that returns a SharedPreference instance poiting to the file that contains the values of preferences. The first parameter is the key and the second parameter is the MODE as shown below,
shared preferences = getSharedPreferences(mypreference,Context.MODE_PRIVATE);
Step 2 : Save object to shared preferences
We can save data to shared preferences using SharedPreferences.Editor class, for which we need to use edit method of the shared preference.
We can save data to shared preferences using SharedPreferences.Editor class, for which we need to use edit method of the shared preference.
Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Email, e);
editor.commit();
Step 3 : Get data from shared preference
shared preferences.getString() method should be used to get the values for corresponding keys as shown below
if (sharedpreferences.contains(Name)) {
sharedpreferences.getString(Name, "");
}
if (sharedpreferences.contains(Email)) {
sharedpreferences.getString(Email, "");
}
So basically using these get and put methods in shared preference we can perform this save and retrieval functionality as we wish. SharedPreferences are not the only way to store data. SQLite, AccountManager are some of the alternatives.
But which one to use and when ??
Well, If you want to store some user-preference data - SharedPreferences may be a good choice.
-
If you want to store authentication data, like your user's auth-tokens - don't use SharedPreferences and take a look at AccountManager instead.
-
If you want to store business data e.g. multiple business entities, that keep some relations to each other, you want to be able to query on them and/or modify it - I'd recommend you to use Realm. Alternative is to use SQLite but in my very subjective opinion, Realm is much easier to start with.
-
If you just want to cache some JSON-based responses - take a look at caching mechanisms that your HTTP client may offer you.
OkHttpfor instance, has a pretty good support for that.
Speaking of loading time - SharedPreferences is pretty fast in general, but it really depends on how you use it. If you store big JSON structs in it, then read it fully just to find some specific object based on id obviously it'll take more time than using a real database for that.
Have in mind that all solutions I proposed (AccountManager
, SharedPreferences, and SQLite/Realm) can perfectly work with each other in one app. Just make sure to choose the proper tool for a given problem.
No comments:
Post a Comment