7

I'm trying to (correctly) implement a preferences screen, but the problem is that all the methods used to read preferences from xml files are deprecated (or I just don't recognize them). The official sample code on the dev site (PreferenceActivity) uses deprecated methods. Has anyone found out a way to implement a preferences screen with an xml file but without using either: addPreferencesFromResource(int) or findPreference(CharSequence)? Or have the methods just been marked deprecated without implementing the alternative yet?

EDIT: Developing for Android version 2.1

SBoss
  • 8,845
  • 7
  • 28
  • 44
  • 1
    From which version do you want to support? As I see it has been changed since 11. If you are using anything below this, then it is not deprecated for them. – Vinay Jun 28 '11 at 08:37

2 Answers2

6

Why its deprecated and what is the alternative is pretty well explained in documentation:

This is the base class for an activity to show a hierarchy of preferences to the user. Prior to HONEYCOMB this class only allowed the display of a single set of preference; this functionality should now be found in the new PreferenceFragment class. If you are using PreferenceActivity in its old mode, the documentation there applies to the deprecated APIs here.

In other words, if you want to be HONEYCOMB compliant, then you should use PreferenceFragment for your PreferenceActivity. A detailed explanation on how to use fragments can be found in dev guide.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • 2
    Seems unintuitive still. Why is it important to support fragments of preferences instead of preference? What prevents Android to deprecate Layout or View in favor of LayoutFragment or ViewFragment?? – KalEl Nov 06 '11 at 23:13
  • First, layouts are views. So you're basically asking about `ViewFragment`. And, well, your question is weird. Because `View`s are are totally different creatures, and they semantically have nothing in common with `Activities` or `Fragments`. Therefore there is no sense in having `ViewFragment`. And if there were reasons to have it, this is currently technically imposible: `Fragments` can not contain subfragments, while `Views` can be arranged in a sophisticated tree hierarchy. – inazaruk Nov 07 '11 at 07:32
  • that's great, but this doesn't tell me how it will automagically store my applications preferences, like the deprecated classes do – slinden77 Aug 01 '12 at 14:54
  • @inazaruk Does "display of a single set of preference" means if i want single,there is no problem? – Dr.jacky Nov 10 '12 at 11:14
6

In Android 3, API Level 11, the fragment-based preference model was introduced, thus deprecating methods that "is not relevant for a modern fragment-based PreferenceActivity."

Since the online reference is the latest version, it shows the methods as deprecated. By manipulating the API Level dropdown, you can mark the methods that are not in the given Android version, but it doesn't update the descriptions to match, which is why it still shows up as deprecated.

If you don't plan on supporting Android 3+ you should just use the old methods, as the fragment-based solutions will not work to versions prior to this.

Flygenring
  • 3,818
  • 1
  • 32
  • 39
  • but how can I do preferences to support Android 3+ and 3- ?? is it possible?? findpreference doesn´t work for 3+, but fragment-based preference doesn´t work for 3-... – Viker Feb 14 '13 at 00:01
  • 1
    To support both, I would make an implementation for versions that support fragment-based preferences and one for those that don't, and then determine which to use by using `if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)`. Also check the [documentation](http://developer.android.com/reference/android/os/Build.VERSION.html) ;) – Flygenring Feb 14 '13 at 14:22
  • I was trying to avoid that option but I´m afraid it is the only one. Thanks for your answer! – Viker Feb 14 '13 at 19:11
  • Well, if you want proper support it is the right way, as fragments aren't supported before version 3 and the previous method has been deprecated since version 3. You could just do it the old way and hope that the methods are still just deprecated and not entirely removed yet, but that's really not viable. – Flygenring Feb 15 '13 at 09:33
  • Yes, I tried with the old method findpreference() in Android 4.1 and it doesn´t run. I will put both ways in the app. – Viker Feb 15 '13 at 22:46