Using Sharedpreferences in android
In this blog post, I would like explain how to use SharedPreferences in Android.
so lets get started.
What is SharedPreferences?
Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.
In order to use shared preferences , you have to call a method getSharedPreferences() that returns a SharedPreference instance poiting to the file that contains the values of preferences.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
The first parameter is the key and the second parameter is the MODE. Apart from private there are other modes availaible that are listed below:
1) MODE_APPEND – This will append the new preferences with the already exisiting preferences
2) MODE_ENABLE_WRITE_AHEAD_LOGGING – Database open flag. When it is set , it would enable write ahead logging by default
3) MODE_MULTI_PROCESS – This method will check for modification of preferences even if the sharedpreference instance has already been loaded
4) MODE_PRIVATE – By setting this mode , the file can only be accessed using calling application
5) MODE_WORLD_READABLE – This mode allow other application to read the preferences
6) MODE_WORLD_WRITEABLE – This mode allow other application to write the preferences
You can save something in the sharedpreferences by using SharedPreferences.Editor class. You will call the edit method of SharedPreference instance and will recieve it in an editor object. Its syntax is:
Editor editor = sharedpreferences.edit(); editor.putString("key", "value"); editor.commit();
Storing Data:
You can save data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. Call editor.commit() in order to save changes to shared preferences.
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 editor.commit(); // commit changes
Retriving Data:
Data can be retrived from saved preferences by calling getString() (For string) method. Remember this method should be called on Shared Preferences not on Editor.
// 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
Clear/Deleting Data:
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()
editor.remove("name"); // will delete key name editor.remove("email"); // will delete key email editor.commit(); // commit changes
Following will clear all the data from shared preferences
editor.clear(); editor.commit(); // commit changes
Check whether SharedPreferences contains any data or not:
sharedpreferences.contains(MyPREFERENCES)
Its Boolean, so it returns either true or false only.
Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.
Related Posts
durga chiranjeevi
Latest posts by durga chiranjeevi (see all)
- Sharing Application Data Between Two Android Apps – Part III - April 8, 2015
- Sharing Application Data Between Two Android Apps – Part II - April 8, 2015
- Sharing Application Data Between Two Android Apps – Part I - April 8, 2015
Watch the Video and get the free raw code:
https://www.youtube.com/watch?v=l5QA9X81FBQ