Awesome
Awesome

Reputation: 457

How to set default values for multiple xml preferences?

There are 2 xml preferences files with separate settings..

PreferenceManager.setDefaultValues(file1)  //works
PreferenceManager.setDefaultValues(file2)  does not work

Second files default values are not loaded and I think that's expected..

How to call PreferenceManager.setDefaultValues for 2 different files, ensuring it will be executed only once..

Upvotes: 0

Views: 848

Answers (3)

EAGLE4YE
EAGLE4YE

Reputation: 346

Put the first false and the second true like this:

PreferenceManager.setDefaultValues(context, R.xml.file1, false);
PreferenceManager.setDefaultValues(context, R.xml.file2, true); 

Upvotes: 0

Awesome
Awesome

Reputation: 457

Ended up with below hack..

final SharedPreferences defaultValueSp = context.getSharedPreferences(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE); 

    if(!defaultValueSp.getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, false))
    {
        PreferenceManager.setDefaultValues(context, R.xml.file1, false);
       //passing true as ignored otherwise because of above call
        PreferenceManager.setDefaultValues(context, R.xml.file2, true); 
    }

Upvotes: 2

fouomene
fouomene

Reputation: 21

You can use Interface Editor for modifying values in a SharedPreferences object. Ex method :

public static void setPreferredArtistName(Context context, String artistName) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("ArtistName", artistName);
        editor.commit();
 }

Upvotes: 1

Related Questions