Separate PreferenceActivity page without headers / snippets?

I am trying to accomplish something that, in my opinion, is pretty simple, but I don’t understand how to do it using the PreferenceActivity class and xml preference header tag.

When the user clicks the application settings icon, I would like them to be represented by a small list of checkboxes and lists. I have only one piece of preference. So far, I can customize the XML PreferenceActivity files (preferences.xml and xmls preference fragments) to show a single header for the preference fragment. When the user selects this title, the settings screen is exactly the way I want it, however I cannot figure out how to skip the display of the first title screen.

It seems like spending time using the settings / preferences icon, showing one heading, which is then used to get the actual settings / preferences.

I understand how this can be very useful if you want to classify your preferences, but for something simple, it adds overhead and seems rather awkward.

I hope I was clear. In short, here is my question:

What is the new preferred way to use the PreferenceActivity class and preference header tag to just show one options screen without a header?

Looking around a little more, it looks like I'm trying to do what the old methods did in a straightforward manner. I am trying to do this without using legacy features.

Thanks in advance B.

+48
android android-fragments android-preferences
Jul 08 2018-12-12T00:
source share
2 answers

A new preferred way is to display a single PreferenceFragment as the main content of any activity. This should not be PreferenceActivity . See Demo APIs

 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); } } } 
+74
09 Oct '12 at 19:16
source share

I was looking for an answer that matched this question. In the end, I worked it out from several other sources. For those who may still need an answer, this is what worked for me. (Note. Both the minimum and target SDKs in this project are set to 15.)

  • Drop PreferenceHeaders, you won’t need them.
  • Create a settings screen with one page settings.
  • Create a preference activity class (SettingsActivity below).
  • Create an inline class extending the PreferenceFragment (LocationFragment below).
  • Define the class in the manifest.
  • Run the task - see the menu code below.

A preference class that displays a 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); } } } 

Code for displaying settings:

 @Override public 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 completes the Settings action. The functions entered in the settings save any changes. In the onResume function, I have getSettings (), which updates any changed parameters used by the calling activity (MainActivity in this case).

What is it.

+20
Sep 14 '12 at 22:59
source share



All Articles