SharedPreferences provide a way to save data to disk in the form of key-value pairs.
Context Method
Activity Method
SharedPreferences Methods
SharedPreferences.Editor Methods
Parameter | Details |
---|---|
key | A non-null String identifying the parameter. It can contain whitespace or non-printables. This is only used inside your app (and in the XML file), so it doesn't have to be namespaced, but it's a good idea to have it as a constant in your source code. Don't localize it. |
defValue | All the get functions take a default value, which is returned if the given key is not present in the SharedPreferences . It's not returned if the key is present but the value has the wrong type: in that case you get a ClassCastException . |
SharedPreferences
shouldn't be used for storing large amount of data. For such purposes, it's much better to use SQLiteDatabase
.
SharedPreferences
are single process only, unless you use deprecated mode MODE_MULTI_PROCESS
. So if your app has multiple processes, you won't be able to read main process's SharedPreferences
in another process.
In such cases, you should use another mechanism to share data across processes, but don't use MODE_MULTI_PROCESS
as it is not reliable as well as deprecated.
It's better to use SharedPreferences
instance in Singleton
class to access all over the Application context
. If you want to use it only for particular Activity go for getPreferences()
.
Avoid storing sensitive information in clear text while using SharedPreferences
since it can be read easily.
https://developer.android.com/reference/android/content/SharedPreferences.html