Reputation: 12249
Until now the "Reset to Default" button in the PreferenceActivity of my app did something like this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
That's been working fine, but recently I saw the setDefaultValues method in the PreferenceManager class. The note at the end of the function says that I should call this function after clearing the preferences to set them back to their defaults. So I'm wondering why the documentation is telling me to call this method if everything has been working fine without calling it?
It also doesn't logically seem to be necessary. If you clear the preferences, it's going to do the same thing it did when your application ran for the first time. It's going to use the defaultValue attributes in the preferences to fill in all the values. So it seems redundant to call this extra method.
So is it necessary?
Upvotes: 2
Views: 1257
Reputation: 61
Kindly see the note in that where it says that using true will not erase the set values and reset with the default values. for that purpose you need to first clear the set values and then call PreferenceManager.setDefaultValues to set the default values.
i hope this clears further.
Upvotes: 1
Reputation: 3203
In general, this extra call is necessary, but in your particular case, it is basically redundant.
I may be wrong, but the editor method you are using with editor.clear()
sets all preferences to null, not their defaults. It is not until the user inflates the PreferenceActivity
that the preferences.xml is read and the defaults are set.
Basically, what calling SetDefaultValues()
does is set the preferences to the default value only if the preferences are set to null
. Say you have a preference called "Age", which the default is set in your xml to "21". Assuming the user has never inflated the pref menu,depending on how you query the preferences, it may give you a null response. This is why querying in the form prefs.getString("prefkey", "defaultvalue");
is good practice. Making this call (and passing in true
)prior to the query will set "Age" to the default value of "21". Note: Once the user has set a preference, or inflated the menu, this call has no effect.
However, since you are calling editor.clear()
from your preference activity, the preferences are automatically repopulated with the default values from the preferences.xml, and therefore making the call would not be necessary. However, if you made the call from some other activity, it would be required to do so, or all your preferences will be null. For completeness, I would suggest you add the call just as belt and suspenders, and this way, if you ever decide to move your function out of this activity, you won't be wondering why all your preferences are null. It shouldn't have any impact, performance-wise.
Also, not related, but you could save yourself some lines of code by replacing the last 3 lines in your example with preferences.edit().clear().commit();
Upvotes: 1