Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:15:07+00:00 2026-06-02T10:15:07+00:00

Trying the different preference activities in the ApiDemos for Android 4.0, I see in

  • 0

Trying the different preference activities in the ApiDemos for Android 4.0, I see in the code that some methods are deprecated in PreferencesFromCode.java, for example.

So my question is: if I use PreferenceFragment, will it work for all version or only 3.0 or 4.0 and up?

If so, what should I use that works for 2.2 and 2.3 as well?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:15:11+00:00Added an answer on June 2, 2026 at 10:15 am

    PreferenceFragment will not work on 2.2 and 2.3 (only API level 11 and above). If you want to offer the best user experience and still support older Android versions, the best practice here seems to be to implement two PreferenceActivity classes and to decide at runtime which one to invoke. However, this method still includes calling deprecated APIs, but you can’t avoid that.

    So for instance, you have a preference_headers.xml:

    <preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > 
        <header android:fragment="your.package.PrefsFragment" 
            android:title="...">
            <extra android:name="resource" android:value="preferences" />
        </header>
    </preference-headers>
    

    and a standard preferences.xml (which hasn’t changed much since lower API levels):

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="...">
        ...
    </PreferenceScreen>
    

    Then you need an implementation of PreferenceFragment:

    public static class PrefsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
    

    And finally, you need two implementations of PreferenceActivity, for API levels supporting or not supporting PreferenceFragments:

    public class PreferencesActivity extends PreferenceActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            addPreferencesFromResource(R.xml.other);
        }
    }
    

    and:

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

    At the point where you want to display the preference screen to the user, you decide which one to start:

    if (Build.VERSION.SDK_INT < 11) {
        startActivity(new Intent(this, PreferencesActivity.class));
    } else {
        startActivity(new Intent(this, OtherPreferencesActivity.class));
    }
    

    So basically, you have an xml file per fragment, you load each of these xml files manually for API levels < 11, and both Activities use the same preferences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a Java web based application that can work flow different
I am trying to include different pages on one page. However the problem that
I have android application that is written regular way. layouts, java, APK. Now, depending
I'm trying to get an existing Preference subclass to work that saves a color
I've been trying to use an android preference activity, I got it all set
My application has multiple activities that use the same database in different ways. Periodically,
I'm trying to get a tabbed layout working on android. Using an example I
I am trying to create multiple shortcuts to my application that pass different arguments
I am trying to solve the readers-writers problem with writer preference in Java using
I was trying to use IB in a slightly different way that I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.