Sharedpreferences - Android Training

Sharedpreferences - Android Training  


Android assigns many methods of storing data of an application. One of this method 
is known as Shared Preferences. Shared Preferences allow you to save and retrieve
 data in the form of key,value pair.Shared Preferences is one of the ways to store 
data persistently in Android application. Here the data is stored in Key-Value pair. 
It is simple and easy to device differentiate to other data storage options like sq lite
 database where we need to write lot of codes.
Android Training in Chandigarh
In Shared Preferences the data is stored persistently, It means the stored data will
 remain even when the app is closed and removed from background. The data will 
be free if the app is uninstalled or if the user manually free the app cache from settings.
In order to use shared preferences, you have to call a method getSharedPreferences()
 that returns a SharedPreference instance pointing to the file that contains the values 
of preferences. Shared Preference are used to store the data for application 
in Android Training in Chandigarh.

We have three APIs to access preferences:

  • getPreferences() : used from inside your Activity, to approach activity-specific 
preferences
  • getSharedPreferences() : used from inside your Activity to access application-
level preferences
  • getDefaultSharedPreferences() : This used for the PreferenceManager  to 
get the shared preferences that work in show  with Android global preference framework.
Operating Modes :
  • MODE_PRIVATE: the ordinary mode, where the created file can only be 
approached by the profession application
  • MODE_WORLD_READABLE: Any application can read the preference data.
  • MODE_MULTI_PROCESS: This method will check for modification of 
preferences even if the Shared Preference instance has already been loaded
  • MODE_APPEND: This will attach the current preferences with the previously 
persist preferences
  • MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag. When it is 
set, it would enable write ahead logging by default.
  • MODE_WORLD_WRITEABLE: Any application can edit the preference data. 
This may cause security holes in applications


Android Preferences provide a way to store user and application data in device. 
It stores primitive data including String objects in key-value pairs in 
 Android Training in Chandigarh.
There are various methods to store data in android :
  1. Preferences(Shared and User) : Store private primitive data in key-value pairs.
  2. Internal Storage : Store private data on the device memory.
  3. External Storage : Store public data on the shared external storage.
  4. SQLite database : Stock structured data in a private database. 
  5. Content Provider : Share data with other apps Shared Preferences
Shared Preferences(Permanent Storage):
  • Very simple way of share small amount of data between activities. Also for 
saving the state of the app. Preferences can stocks only primitive kinds of data.
  • Preferences can be private or public (world readable, writeable)(deprecated from Api 17)
  • To get SharedPreferences, use
  • – getSharedPreferences(String name) (if multiple pref. files)
  • – getPreferences() (if only one pref. File is needed)
  • To write values, call edit() to get SharedPreferences.Editor to be used when adding 
values to file.

Learn more about Android shared Element go through this link :- https://goo.gl/e4cte2

0 comments:

Post a Comment