Reputation: 2835
I am working at API level 9 so can not use the new Preference Fragment stuff.
I have a preference activity getting the preferences from and xml file as per the api demos. I have a hierarchy of preference screens so normaly to get to a preference you have to click though a top level screen.
As well as getting to all the preferences from my main menu I would like to be able to start the preference activity at one of the sub screens rather than the top screen so I can start it directly in one of the sub screens on say a click in an activity that that group of sub preferences is relervant to.
Anyone know if this is posible? I would of thought that passing some data with the intent would be the way but I can not find anything saying this is possible.
Upvotes: 0
Views: 2945
Reputation: 2835
OK here is what I have ended up with with Kurtis's help.
Basically in my code starting the Preferences activity I have no action for all the preferences and an action if you want just some of them. The action needs to match the key on a preference or preferenceGroupe of some sort.
// all preferences
Intent launchPreferencesIntent = new Intent().setClass(this,
PreferencesFromXml.class);
startActivity(launchPreferencesIntent);
// just key_trip_plot_control_preferences
Intent launchPreferencesIntent = new Intent(
getString(R.string.key_trip_plot_control_preferences))
.setClass(this, PreferencesFromXml.class);
startActivity(launchPreferencesIntent);
In my PreferencesFromXml class I always add the preferences from the xml but then if I have an action I search though the preferences looking for a matching key. If I find one I removeAll preferences then add the matching one or it's children if it is a PreferenceGroupe back in.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
String act = getIntent().getAction();
if (act != null) {
Preference res = findPreferenceByKey(getPreferenceScreen(), act);
if (res != null) {
getPreferenceScreen().removeAll();
if (res instanceof PreferenceGroup) {
PreferenceGroup groupe = (PreferenceGroup) res;
// add sub items
for (int i = 0; i < groupe.getPreferenceCount(); i++) {
Preference pref = groupe.getPreference(i);
if (pref != null) {
getPreferenceScreen().addPreference(pref);
}
}
} else { // just add the item.
getPreferenceScreen().addPreference(res);
}
}
}
}
protected Preference findPreferenceByKey(PreferenceGroup in, String key) {
for (int i = 0; i < in.getPreferenceCount(); i++) {
Preference pref = in.getPreference(i);
if (pref == null) {
// should not happen
Log.v(TAG, "findPreferenceByKey null pref i:" + i);
return null;
} else if (pref.hasKey() && pref.getKey().equals(key)) {
return pref;
} else if (pref instanceof PreferenceGroup) {
// recurse
Preference res = findPreferenceByKey((PreferenceGroup) pref,
key);
if (res != null) {
return res;
}
}
}
return null;
}
Upvotes: 1
Reputation: 30825
First of all, the Preference Fragment is available to you through the android compatibility package: http://developer.android.com/sdk/compatibility-library.html
Second, yes this is possible. When you're creating your fragment or acitvity (which ever you decide), use the addPreferencesFromIntent() method ( http://developer.android.com/reference/android/preference/PreferenceFragment.html#addPreferencesFromIntent%28android.content.Intent%29 ), and instead of adding the whole screen just add the subscreen you want.
Upvotes: 0