0

I have a settings.xml file that contains Preferences for my app. All the values by default are set to "true" and its used by PreferenceActivity in my app. In my main activity I read the values through

     SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(appContext);
     Boolean key = sp.getBoolean("M", false);

it gets me whatever I need at any run except the first time. Only when I open my app for the first time and doesn't open my settings menu I get "false". I mean I have to open menu and only after that the app run correctly.

Any suggestions?

user1927829
  • 302
  • 3
  • 9
  • How do you move the values from settings.xml to the shared preferences without running PreferenceActivity? – Henry Dec 25 '12 at 08:13

2 Answers2

2

Because yor are getting the default value for first time. that is false -->

Boolean key = sp.getBoolean("M", false);

use this

Boolean key = sp.getBoolean("M", true);
slfan
  • 8,950
  • 115
  • 65
  • 78
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
0

Use sp.getBoolean("M", true); instead. Default values in preferences file is what it will be when preferences activity is first started. SharedPreferences know nothing about your settings.xml.

Alternatively you can have a separate defaults.xml file (or any other name) containing all the default values for all preferences. Then you use these values in both settings.xml (@bool/default_M_value) and your application (sp.getBoolean("M", getResources().getBoolean(R.bool.default_M_value))).

I personally would stick to first approach though.

aragaer
  • 17,238
  • 6
  • 47
  • 49