Skip to content Skip to sidebar Skip to footer

How Do I Call A Specific Preferencefragment From A Preferenceactivity?

I have a PreferenceActivity with several fragments: R.xml.preferences: (shortened for better readability):

Solution 1:

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 = newIntent( this, SettingsActivity.class );
intent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT, Fragment1.class.getName() );
intent.putExtra( PreferenceActivity.EXTRA_NO_HEADERS, true );

Solution 2:

Just use below code in your PreferenceActivity and PreferenceFragment in onCreate

addPreferencesFromResource(R.xml.**YOUR PREFERENCE FRAGMENT XML**);

Solution 3:

Or use PreferenceActivity.switchToHeader. When starting the PreferenceActivity, the header/overview of all settings pages is loaded first and then the fragment. When finishing the fragment, you will return to the overview.

MyPreferenceActivity:

@OverridepublicvoidonBuildHeaders(List<Header> headers) {

    loadHeadersFromResource(R.xml.preference_headers, headers);

    intheaderId= getIntent() != null ? getIntent().getIntExtra("header", 0) : 0;
    if (headerId > 0) {
        getIntent().removeExtra("header");
        switchToHeader(findHeaderById(headerId));
    }
}

private Header findHeaderById(long id) {

    if (headers != null)
        for (Header header : headers)
            if (header.id == id) return header;

    returnnull;
}

This shows how to call the PreferenceActivity together with the fragment in your MainActivity:

Intent intent = newIntent(context, MyPreferenceActivity.class);
intent.putExtra("header", R.id.header1);
intent.putExtra("key", "pref1");
startActivity(intent);

If necessary, you can open the respective preference right away in PreferenceFragment1:

@OverridepublicvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences_1);  

    Stringkey= getActivity().getIntent() != null ? getActivity().getIntent().getStringExtra("key") : null;

    if (key != null) {
         getActivity().getIntent().removeExtra(key);
         openPreference(key);
    }
}

privatevoidopenPreference(String key) {

    PreferenceScreenpreferenceScreen= getPreferenceScreen();
    ListAdapterlistAdapter= preferenceScreen.getRootAdapter();

    for (intposition=0; position < listAdapter.getCount(); position++) {

        Preferencepreference= (Preference) listAdapter.getItem(position);

        if (preference.equals(findPreference(key))) {
            preferenceScreen.onItemClick(null, null, position, 0);
            break;
        }
    }
}

preference_headers.xml:

<?xml version="1.0" encoding="utf-8"?><preference-headersxmlns:android="http://schemas.android.com/apk/res/android"><headerandroid:id="@+id/headers1"android:fragment="de.almisoft.PreferenceFragment1"android:title="Title1"android:summary="Summary1"></header><headerandroid:id="@+id/headers2"android:fragment="de.almisoft.PreferenceFragment2"android:title="Title2"android:summary="Summary2"></header></preference-headers>

preferences_1.xml:

<?xml version="1.0" encoding="utf-8"?><PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"><Preferenceandroid:key="pref1"android:title="title"/></PreferenceScreen>

Post a Comment for "How Do I Call A Specific Preferencefragment From A Preferenceactivity?"