Reputation: 881
I have multiple PreferenceActivity classes because My main PreferenceActivity XML is essentially an index into multiple kinds of preferences.
I am looking for help on how to set the preference defaults from the XML when the app is first run after installation. I have unsuccessfully tried the following (from main activity onCreate()):
PreferenceManager.setDefaultValues(ctx, R.xml.prefs, false);
PreferenceManager.setDefaultValues(ctx, R.xml.prefsdisplay, false);
PreferenceManager.setDefaultValues(ctx, R.xml.prefsloc, false);
PreferenceManager.setDefaultValues(ctx, R.xml.prefsmaps, false);
PreferenceManager.setDefaultValues(ctx, R.xml.prefsmisc, false);
Where R.xml.prefs is the master preferences file, and the others are sub-level preferences.
Note that I have two preferences activities, one that uses R.xml.prefs as its descriptor:
addPreferencesFromResource(R.xml.prefs);
and the other that uses whatever xml file name is passed in its startup intent. (BTW, other ways to handle this that are compatible down to 1.6 would be of interest):
Intent intent = this.getIntent();
String data = intent.getDataString();
String pkgName = getPackageName();
int resID = getResources().getIdentifier(data , "xml", pkgName);
addPreferencesFromResource(resID);
FILES: R.xml.prefs is:
<PreferenceCategory android:title="Map Settings">
<PreferenceScreen android:title="Map Display Settings"
android:summary="Control Location Settings">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.mycompany.app"
android:targetClass="com.mycompany.app.app.SubPrefsAct"
android:data="prefsloc"/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
And prefsloc.xml is:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="GPS and Location">
<CheckBoxPreference android:key="ShowLocation"
android:summary="Shows location symbol on map" android:defaultValue="true"
android:title="Show Location"></CheckBoxPreference>
<CheckBoxPreference android:key="UseGps"
android:summary="Uses GPS to refine position" android:title="Use GPS"
android:defaultValue="true"></CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
Upvotes: 0
Views: 1304
Reputation: 881
Answering my own question: use true instead of false in setDefaultValues(...) calls.
Otherwise the call for the first XML file will cause the remaining calls to not do anything.
Upvotes: 1