Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

android - How do I call a specific PreferenceFragment from a PreferenceActivity?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to: http://developer.android.com/reference/android/preference/PreferenceActivity.html#EXTRA_SHOW_FRAGMENT

public static final String EXTRA_SHOW_FRAGMENT

Added in API level 11 When starting this activity, the invoking Intent can contain this extra string to specify which fragment should be initially displayed.

Constant Value: ":android:show_fragment"

intent = new Intent( this, SettingsActivity.class );
intent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT, Fragment1.class.getName() );
intent.putExtra( PreferenceActivity.EXTRA_NO_HEADERS, true );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...