PreferenceFragment - Difference between getPreferenceManager() and getPreferenceScreen()?
Asked Answered
T

3

37

I've implemented my own PreferenceFragment subclass (detailed here), and want to listen for preference changes within it. PreferenceFragment provides you with two ways of doing this:

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

and

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

Which one should be used? What's the difference? I don't really understand the distinction made in the Android docs.

Tokharian answered 29/11, 2012 at 3:26 Comment(3)
From what I understand from the API, it seems that Screen is more specific and is not really guaranteed to be present whereas Manager should be available more often. +1 for a nice question!Yean
Just to clarify regarding the renewed bounty, what is it you want to know beyond what Shruti and JoseLSegura have already answered.Hovercraft
Shruti's answer is largely copy/paste from the API docs, which I have already read several times. Jose's answer is more what I am looking for, but it does not seem authoritative enough. I'd simply like others to weigh in on this.Handicraftsman
T
18

The core difference is in their names, PreferenceManger grants access to different functionalities to the developer for managing SharedPreferences, such as retrieving the map of current preference values or setting user preferences. to their default values. PreferenceScreen handles displaying a screen of user preferences, so that the user can assign values to them. Sometimes this means displaying a list item on a screen with other preferences, that opens another screen with more preferences when clicked, as is the case when PreferenceScreens are nested.

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

PreferenceScreen:

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

     return mPreferenceManager.getSharedPreferences();
 }

So the PreferenceManger and PreferenceScreen are different entities, but the SharedPreference those method return should be the same object, since PreferenceScreen calls the method from PreferenceManager. I hope that is the answer you've been seeking.

If you have a choice, go with PreferenceManager.getSharedPreferences(), it's more obvious and one fewer method call internally.

Fun fact:

PreferenceFragment:

public PreferenceManager getPreferenceManager() {
    return mPreferenceManager;
}

public PreferenceScreen getPreferenceScreen() {
    return mPreferenceManager.getPreferenceScreen();
}
Triangle answered 18/12, 2012 at 22:4 Comment(3)
Thanks, this is precisely the answer I was looking for. I didn't even think to look at the Android source code.Handicraftsman
Please share source code link or help about how to explore? In PreferenceScreen.java there is no method named getSharedPreferences(). I have explored from below link. android.googlesource.com/platform/frameworks/support/+/bd4cbab/…Jesu
find here android.googlesource.com/platform/frameworks/support/+/bd4cbab/…Jesu
I
16

The first one gets the shared preferences from the PreferenceManager. The second one, from the PreferenceScreen, that inherits this method from Preference class.

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

Ieyasu answered 2/12, 2012 at 23:36 Comment(0)
C
13

PreferenceScreen see domentation here

PreferenceScreen class can appear in two places:

  • When a PreferenceActivity points to this, it is used as the root and is not shown (only the contained preferences are shown).
  • When it appears inside another preference hierarchy, it is shown and serves as the gateway to another screen of preferences (either by showing another screen of preferences as a Dialog or via a
    startActivity(android.content.Intent) from the getIntent()). The
    children of this PreferenceScreen are NOT shown in the screen that
    this PreferenceScreen is shown in. Instead, a separate screen will be shown when this preference is clicked.

PreferenceManager see documentation here.

Difference :

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

getPreferenceScreen () returns the root PreferenceScreen i.e. root preference screen used in the fragment from preference xml file(preferences.xml).

Colligate answered 3/12, 2012 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.