PreferenceFragment - Difference between getPreferenceManager () and getPreferenceScreen ()?

I implemented my own subclass of PreferenceFragment (detailed here ) and want to listen to the preference changes inside it. PreferenceFragment provides you two ways to do this:

getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); 

and

 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); 

Which one should I use? What's the difference? I really don't understand the differences in Android docs .

+37
android android-preferences android-sharedpreferences
Nov 29 '12 at 3:26
source share
3 answers

The main difference is in their names, PreferenceManger gives the developer access to various functions for managing SharedPreferences , for example, to get a map of current preference values ​​or customize user settings. to their default values. PreferenceScreen handles the display of user preferences so that the user can assign values ​​to them. Sometimes this means displaying a list item on a screen with different settings, which opens another screen with a lot of preferences when clicked, as in the case when PreferenceScreen are nested.

Your question implies that you think there is a difference between what PreferenceManager.getSharedPreferences() and PreferenceScreen.getSharedPreferences() , but they are identical according to the source code.

PreferenceScreen :

 public SharedPreferences getSharedPreferences() { if (mPreferenceManager == null) { return null; } return mPreferenceManager.getSharedPreferences(); } 

Thus, PreferenceManger and PreferenceScreen are different entities, but SharedPreference returns this method as the same object, since PreferenceScreen calls the method from PreferenceManager . Hope this is the answer you were looking for.

If you have a choice, go to PreferenceManager.getSharedPreferences() , this will become more obvious and will call one method inside.

Fun fact:

PreferenceFragment :

 public PreferenceManager getPreferenceManager() { return mPreferenceManager; } public PreferenceScreen getPreferenceScreen() { return mPreferenceManager.getPreferenceScreen(); } 
+17
Dec 18
source share

The first gets the general settings from the PreferenceManager . The second is from PreferenceScreen , which inherits this method from the Preference class.

I think this is not a functional difference, because both probably return the same instance of SharedPreferences objects, but I think it's better to use the first one (using PreferenceManager instead of PreferenceScreen ).

+16
Dec 02
source share

PreferenceScreen see domentation here

The PreferenceScreen class can appear in two places:

  • When the PreferenceActivity function indicates this, it is used as the root and is not displayed (only the indicated preferences are displayed).
  • When it appears inside a different hierarchy of preferences, it is displayed and serves as a gateway to another preference screen (either by showing another preference screen in the form of a dialog or through startActivity (android.content.Intent) from getIntent ()). The children of this preference screen are NOT displayed on the screen that this preference screen is shown in. Instead, a separate screen will be displayed when you click this preference.

PreferenceManager see documentation here :

Difference :

getPreferenceManager () returns the current preference manager associated with the fragment.

getPreferenceScreen () returns the root of the PreferenceScreen , i.e. The root preferences screen used in the snippet from the XML preferences file (preferences.xml).

+13
Dec 03
source share



All Articles