Shared Preferences are used
in Android to store data. These data can be used in appropriate places in the
application from reading them.
The shared preferences allows
us to manage a session. Session is useful when need to store user data globally
throughout the application. This can be done storing the data in shared
preferences.
Shared Preferences is an
API which includes in Android SDK to store and retrieve application
preferences. Shared Preferences are simply sets of data in key value pair that
stored persistently. Furthermore if the application stop or turn off the device
will not clear the data stored in shared preferences. Shared Preferences available
at the Activity level or shared across all Activity in application package.
Shared Preferences are
used in android to store some data persistently so when you need to store few
amount of data then you can use Shared Preferences instead of SQLite.
But shared preference is
not a solution to keep complex relational data.
The following basic information
will be needed when you use the Shared
Preferences in your application.
Store Data
in Shared Preferences.
All the primitive data
types like boolean, floats, int, longs, and strings can store in Shared
Preferences using following methods.
editor.putBoolean("key_name", true);
// Storing boolean - true/false
editor.putString("key_name",
"string value"); // Storing string
editor.putInt("key_name", "int
value"); // Storing integer
editor.putFloat("key_name",
"float value"); // Storing float
editor.putLong("key_name", "long
value"); // Storing long
Retrieve
data from Shared Preferences.
All the primitive data
types like boolean, floats, int, longs, and strings can retrieve from Shared
Preferences using following methods.
// returns stored preference value
// If value is not present return second param
value - In this case null
pref.getString("key_name", null); //
getting String
pref.getInt("key_name", null); //
getting Integer
pref.getFloat("key_name", null); //
getting Float
pref.getLong("key_name", null); //
getting Long
pref.getBoolean("key_name", null); //
getting boolean
Delete data
from Shared Preferences
If you want to delete
from shared preferences you can call remove(“key_name”) to delete that
particular value. If you want to delete all the data, call clear().
Eg :- editor.remove(“email”)
– this will remove the key email from your Shared Preference.
Editor.clear() – this will
clear all the data from shared preferences.
You can save all the
changes in Shared Preferences using commit().
The following example method
implemented to show how to store data in the Shared Preferences.
public static void
saveGlobalStringPreferences(final Activity activity,
final String key, final String value) {
SharedPreferences
sp = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
Editor
editor = sp.edit();
editor.putString(key,
value);
editor.commit();
}
As you can see above, after
storing a particular data in shared preferences need to commit it to save the
changes successfully.
The following example method implemented to show how
to retrieve data from the Shared Preferences.
public static String
readGlobalStringPreferences(final Activity activity,
final String key, final String defaultValue) {
SharedPreferences
sp = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
return sp.getString(key,
defaultValue);
}
The following example method implemented to show how
to delete data from the Shared Preferences.
public static void
removeGlobalPreferences(final Activity activity,
final String key) {
SharedPreferences
sp = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
Editor
editor = sp.edit();
editor.remove(key).commit();
}
As you can see above, after
removing a particular data from shared preferences need to commit it for
successfully deletion.
For more details Click Here
No comments:
Post a Comment