Single page PreferenceActivity w/no headers/fragments? Single page PreferenceActivity w/no headers/fragments? android android

Single page PreferenceActivity w/no headers/fragments?


New preferred way is to show a single PreferenceFragment as the main content of any activity. It doesn't need to be PreferenceActivity. See the APIs demo sample

public class FragmentPreferences extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // Display the fragment as the main content.        getFragmentManager().beginTransaction().replace(android.R.id.content,                new PrefsFragment()).commit();    }    public static class PrefsFragment extends PreferenceFragment {        @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            // Load the preferences from an XML resource            addPreferencesFromResource(R.xml.preferences);        }    }}  


I was looking for an answer that matched this question. Eventually, I worked it out from several other sources. For those that may still want it answered, here's what worked for me. (Note - both min and target SDKs are set to 15 in this project.)

  1. Dump the PreferenceHeaders, you won't need them.
  2. Create a preference screen with the single page settings.
  3. Create a preference activity class (SettingsActivity below).
  4. Create an inline class extending PreferenceFragment (LocationFragment below).
  5. Define the class in the Manifest.
  6. Start the task - see the menu code below.

The preference class that displays the single settings screen.

public class SettingsActivity extends PreferenceActivity {    private final static String TAG = "SettingsAcitivity";    public SettingsActivity() {}    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        MyLog.d(TAG, "onCreate");        getFragmentManager().beginTransaction()                .replace(android.R.id.content, new LocationFragment()).commit();    }    public class LocationFragment extends PreferenceFragment {        private final static String TAG = "LocationFragment";        @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            MyLog.d(TAG, "onCreate");            addPreferencesFromResource(R.xml.locationsettings);        }    }}

The code to display the Settings:

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    MyLog.d(TAG, "onOptionsItemSelected");    switch (item.getItemId()) {    case R.id.menu_main_help:        break;    case R.id.menu_main_about:        break;    case R.id.menu_main_settings:        MyLog.d(TAG, "Settings");        Intent settingsIntent = new Intent(this, SettingsActivity.class);        startActivity(settingsIntent);        break;    }    return true;}

The Back key terminates the SettingsActivity. The built in preference routines save any changes. The onResume function I have does a getSettings() that updates any changed settings used by the calling activity (MainActivity in this case).

That's it.