The first thing we require to do is to create one shared preferences file per app. So name it with the package name of your app- unique and easy to associate with the app. When you want to obtain the values, call the getSharedPreferences() method. Shared Preferences provide modes of storing the data (private mode and public mode). It is for backward compatibility- use only MODE_PRIVATE to be secure.
data persistence
Persistence is "the persistence of an effect after its cause is removed". In the context of storing data in a computer system, this means that the data survives after the process with which it was created has ended. In other words, for a data store to be considered persistent, it must write to non-volatile storage.
stores provide persistence
If you need persistence in your data store, then you need to also understand the four main design methods that a data store can take and how (or if) these designs provide persistence: Pure in-memory, no persistence at all, such as Memcached or Scalaris
In-memory with periodic snapshots, such as Oracle Coherence or Redis
Disk-based with update-in-place writes, such as MySQL ISAM or MongoDB
Commitlog-based, such as all traditional OLTP databases (Oracle, SQL Server, etc.)
In-memory approaches can achieve blazing speed, but at the cost of being limited to a relatively small data set. Most workloads have a relatively small "hot" (active) subset of their total data; systems that require the whole dataset to fit in memory rather than just the active part are fine for caches but a bad fit for most other applications. Because the data is in memory only, it will not survive method termination. Therefore these types of data stores are not considered persistent.
Access using shared preference
After the data is saved, you can access it anywhere in your application through any of the following methods based on the data type:
i. getInt()
ii. getString()
iii. getBoolean()
iv. getLong()
v. getFloat()
vi. getStringSet()
These methods take two parameters each:
The key: The name of the file the data was stored in.
Default value: A default value is taken if the shared preference object is unable to locate data with the provided key.
Following are the methods of Shared Preferences contains
edit(): This method is used to create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.
getAll(): This method is used to retrieve all values from the preferences.
getBoolean(String key, boolean defValue): This method is used to retrieve a boolean value from the preferences.
getFloat(String key, float defValue): This method is used to retrieve a float value from the preferences.
getInt(String key, int defValue): This method is used to retrieve an int value from the preferences.
getLong(String key, long defValue): This method is used to retrieve a long value from the preferences.
getString(String key, String defValue): This method is used to retrieve a String value from the preferences.
getStringSet(String key, Set defValues): This method is used to retrieve a set of String values from the preferences.
registerOnSharedPreferencechangeListener(SharedPreferences.OnSharedPreferencechangeListener listener): This method is used to registers a callback to be invoked when a change happens to a preference.
unregisterOnSharedPreferencechangeListener(SharedPreferences.OnSharedPreferencechangeListener listener): This method is used to unregisters a previous callback.