I just curious. There are 3 method:
1. getPreferenceManager().setSharedPreferencesName(String PrefName); 2. PreferenceManager.getDefaultSharedPreferences(Context context) 3. Context.getSharedPreferences (String name, int mode)
As I know, the third method is only used when the first method is used, right? But with 3 method we also use addPreferencesFromResource(int resID)
; so, what is the difference? When can we use one of these method? Thanks!
getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
getSharedPreferencesMode. Deprecated: Deprecated in Java. Returns the current mode of the SharedPreferences file that preferences managed by this will use. The mode that can be passed to Context#getSharedPreferences(String, int) .
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.
This example demonstrates how to use shared preferences in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Let's go one step at a time:
setSharedPreferencesName()
is method that allows to set the name of the preference group for later use. This is helpful for example when using the helper class of PreferencesActivity before loading a preferences from XML resource file by calling addPreferencesFromResource()
. It is therefore not as common as the other 2 methods you mentioned above.
getDefaultSharedPreferences()
uses a default name, usually stored as /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml. It is commonly used. Note that this default is set per application.
The alternative method - getSharedPreferences()
requires to indicate a specific preference (file) name and an operation mode.
As appears also in another answer about shared preferences, getDefaultSharedPreferences()
in fact uses Context.getSharedPreferences, so the result is the same, but without the flexbility to split to multiple preference files, that is offered by getSharedPreferences()
. Sharing the preferences between apps using a MODE_WORLD_READABLE
operation indicator is also something possible using getSharedPreferences()
, but is rarely used.
IMHO, getDefaultSharedPreferences() can be safely used without going into the confusion of multiple preference file names that are prone to typos and confusion.
If someone knows of a good reason to use getSharedPreferences() and not getDefaultSharedPreferences(), please let me know by commenting here.
getDefaultSharedPreferences()
uses a default preference-file name like "com.example.something_preferences"
. This default is set per application, so all activities in the same app context can access it easily as in the following example:
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this); if (spref.contains("email")) { String sEmailAddr = spref.getString("email", ""); }
The preferences are usually stored at /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml
getSharedPreference is the best way because using getDefaultSharedPreferences has some flaws
getDefaultSharedPreferences
doesn't work correct on some devices when build with targer api 13DefaultSharedPreferences
. After removing DefaultSharedPreferences from my code - it works perfect. I can't just say: people dont make shrotcuts, so I had to change codeThis link may also help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With