Android Preferences (3) display interface Preferenceactivity and preferencefragment after starting preferences

Source: Internet
Author: User

Creating a Preference Activity

To display your settings in an activity, extend the PreferenceActivity class. This is an extension of the traditional Activity class, displays a list of settings based on a hierarchy of Preference objects . The PreferenceActivity automatically persists the settings associated with each of the Preference user makes a change.

Note: If you ' re developing your application for Android 3.0 and higher, you should instead use PreferenceFragment . Go to the next sections about Using Preference fragments.

The most important thing to remember are that and does not load a layout of views during the onCreate() callback. Instead, you call to add the preferences you've declared in a addPreferencesFromResource() XML file to the activity. For example, here's the bare minimum code required for a functional PreferenceActivity :

 Public class extends preferenceactivity {    @Override    publicvoid  onCreate (Bundle Savedinstancestate) {        super. OnCreate (savedinstancestate);        Addpreferencesfromresource (r.xml.preferences);    }}

This was actually enough code for some apps, because as soon as the user modifies a preference, the system saves the change s to a default SharedPreferences file, your other application, can read when you need to check the user's settings. Many apps, however, require a little more code on order to listen for changes that occur to the preferences. For information about listening SharedPreferences to changes in the file, see the sections about Reading Preferences.

Using Preference Fragments

If you ' re developing-Android 3.0 (API level one) and higher, you should use a to PreferenceFragment display your list of Preference objec Ts. You can add a to any PreferenceFragment activity-you don ' t need to use PreferenceActivity .

Fragments provide a more flexible architecture for your application, compared to using activities alone, no matter what Ki nd of activity you ' re building. As such, we suggest you use the PreferenceFragment control the display of your settings instead of when PreferenceActivity possible.

Your implementation of can is as simple as PreferenceFragment defining the onCreate() method to load a preferences file with addPreferencesFromResource() . For example:

 Public Static class extends preferencefragment {    @Override    publicvoid  onCreate (Bundle Savedinstancestate) {        super. OnCreate (savedinstancestate);         // Load the preferences from an XML resource         Addpreferencesfromresource (r.xml.preferences);    }    ...}

You can then add the this fragment to a just as you would for any other Activity Fragment . For example:

 Public class extends Activity {    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);         // Display The fragment as the main content.         Getfragmentmanager (). BeginTransaction (                )new settingsfragment () )                . Commit ();    }}

Note: A PreferenceFragment doesn ' t has a its own Context object. If you need a Context object, you can call getActivity() . However, being careful to call only when the fragment was attached to an getActivity() activity. When the fragment isn't yet attached, or was detached during the end of its lifecycle, would getActivity() return null.

Android Preferences (3) display interface Preferenceactivity and preferencefragment after starting preferences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.