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.
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.
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.
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.
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
- getSharedPreferences() : used from inside your Activity to access application-
- getDefaultSharedPreferences() : This used for the PreferenceManager to
Operating Modes :
- MODE_PRIVATE: the ordinary mode, where the created file can only be
- MODE_WORLD_READABLE: Any application can read the preference data.
- MODE_MULTI_PROCESS: This method will check for modification of
- MODE_APPEND: This will attach the current preferences with the previously
- MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag. When it is
- MODE_WORLD_WRITEABLE: Any application can edit the preference data.
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.
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 :
- Preferences(Shared and User) : Store private primitive data in key-value pairs.
- Internal Storage : Store private data on the device memory.
- External Storage : Store public data on the shared external storage.
- SQLite database : Stock structured data in a private database.
- 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
- 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
Learn more about Android shared Element go through this link :- https://goo.gl/e4cte2