Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when use getDefaultSharedPreferences and getSharedPreferences

Tags:

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!

like image 243
Toan Tran Van Avatar asked Feb 21 '12 20:02

Toan Tran Van


People also ask

What is the difference between the getSharedPreferences () and getPreferences () methods?

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.

Is SharedPreferences deprecated?

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) .

What is Android shared preference?

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.

How do I use Kotlin shared preferences?

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.


2 Answers

Let's go one step at a time:

  1. 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.

  2. 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.

  3. 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.

like image 82
wiztrail Avatar answered Oct 05 '22 14:10

wiztrail


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

  1. Actualy getDefaultSharedPreferences doesn't work correct on some devices when build with targer api 13
  2. Starting app from shortcut and from menu gives me different DefaultSharedPreferences. After removing DefaultSharedPreferences from my code - it works perfect. I can't just say: people dont make shrotcuts, so I had to change code

This link may also help

like image 42
Zar E Ahmer Avatar answered Oct 05 '22 14:10

Zar E Ahmer