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?
PreferenceFragmentwill 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 twoPreferenceActivityclasses 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:and a standard
preferences.xml(which hasn’t changed much since lower API levels):Then you need an implementation of
PreferenceFragment:And finally, you need two implementations of
PreferenceActivity, for API levels supporting or not supportingPreferenceFragments:and:
At the point where you want to display the preference screen to the user, you decide which one to start:
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.