I have a PreferenceActivity with several fragments:
R.xml.preferences: (shortened for better readability):
<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:fragment="fragments.Fragment1" android:id="@+id/fragment1" [...] />
<header android:fragment="fragments.Fragment2" android:id="@+id/fragment2" [...] />
[...]
</preference-headers>
SettingsActivity:
public class SettingsActivity extends PreferenceActivity {
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preferences, target);
}
}
This will show a list entry with fragments.Fragment1
, fragments.Fragment2
, ... if SettingsActivity
is started.
But now I want to pass a Bundle such that a specific PreferenceFragment is opened when starting the activity:
so I added this to SettingsActivity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null && savedInstanceState.getBoolean("shortcut")) {
// directly jump to fragments.Fragment1
}
}
I tried to load the fragment via getFragmentManager().findFragmentById(R.id.fragment1)
, but this returns null
. But even if I had the correct instance, I would not know how to invoke it. Also, calling loadHeadersFromResource(R.xml.preferences_fragment1, target);
does not work - This will throw a RuntimeException "XML document must start with tag; foundPreferenceScreen at Binary XML file". I have no ideas left and also a search on SO and Google did not return any relevant results.
So my question is: Is it possible to directly load a PreferenceFragment (e.g. fragments.Fragment1) from the Activitiy's onCreate method? If so, how?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…