Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select the default header in my PreferenceActivity on tablets?

I am working with a PreferenceActivity that will be fully compatible with tablets.

For this, I will work as advised by Google in this page.

@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.preference_headers, target);
}

My problem is that I would like to be able to select the default header when the activity is launched.

For instance, I have several headers;

  • General Settings
  • UI Settings
  • Network settings

And depending on which activity I come from, I would like to display the correct settings.

Is there a way to achieve that?

like image 924
Waza_Be Avatar asked Jan 19 '12 11:01

Waza_Be


1 Answers

When creating the Intent to invoke the PreferenceActivity, you can add the extra string 'EXTRA_SHOW_FRAGMENT' to specify which fragment should be initially displayed. You pass the name of the fragment you would like to select.

For instance, if you would like to select the General Settings header (and its contents) you can use the following code:

final Intent intent = new Intent(this, ExtendedPreferenceActivity.class); // Assume we call it from an other activty
intent.putExtra(EXTRA_SHOW_FRAGMENT, GeneralSettingsFragment.class.getName());
startActivity(intent);

More information on this can be found here: http://developer.android.com/reference/android/preference/PreferenceActivity.html

In an issue report to Google it is reported that for Android version 3.0 the correct header will not be automatically selected as well. For the issue report and its workaround look here: issue report.

like image 125
dennisg Avatar answered Oct 13 '22 21:10

dennisg