0

According to this I can load the default values from my XML-preference file right at the beginning. But when I check the keys of my settings I see, that my R.xml.preferences is not read at all. Perhaps this will be done the first time, the user launches a PreferenceScreen. Is there a solution reg. How to retrieve the values when user launches the app the first time? Cause the link doesn't help.

Is there some kind of difference when to launch it? But neither Context nor Application brought a change. Or do I need to remove something from the dirs, cause the R.xml.preferences are not installed in the SharedPreferences-Dir from where the content is retrieved?

Community
  • 1
  • 1
LeO
  • 4,238
  • 4
  • 48
  • 88

2 Answers2

0

Use this in onCreate method of your main activity

// Make sure default values are applied.  In a real app, you would
        // want this in a shared function that is used to retrieve the
        // SharedPreferences wherever they are needed.
       PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.fragmented_preferences);
nikhil.thakkar
  • 1,093
  • 7
  • 12
  • For the first - please see the other comment. For the second method, I cannot find them in the `Activty`, but in the `PreferenceActivity` and this would imply to use the PreferenceScreen in advance. But I want to load the preferences for the preference-xml-file in advance prior to user-interaction. – LeO Jun 11 '13 at 07:42
0

Use application class for this:

public class App extends Application{

    @Override
    public void onCreate() {
        super.onCreate();

        PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
    }

}

And add it to manifest:

 <application
            android:name=".App"
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • But this only loads the SharedPrefs which are already used resp. set. I mean they are not retrieved from the build-xml-file but from the xml-which is obviously saved after editing. If I retrieve the settings via `SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);` then I don't get any default-values from the prefs-file. – LeO Jun 11 '13 at 07:38
  • @LeO You have to tell android where to get preferences from. `getDefaultSharedPreferences()` will load empty file if you don't load it before hand. Also, `build.xml` file in project is for Ant, not for app preferences. – S.D. Jun 11 '13 at 09:18
  • Sorry, but I'm lost with your response. It makes sense, but actually I have no clue how to retrieve them. IFFF my (default)preferences are stored in the _R.xml.prefs_ how to retrieve them from there? The `setDefaultValues()`-method might be handy, but I have no clue, how to retrieve/access now the values? – LeO Jun 11 '13 at 14:31