Was PreferenceFragment intentionally excluded from the compatibility package?

James picture James · Mar 31, 2011 · Viewed 38.5k times · Source

I'm looking to write preferences that can be applied to both 3.0 and pre-3.0 devices. Discovering that PreferenceActivity contains deprecated methods (although these are used in the accompanying sample code), I looked at PreferenceFragement and the compatibility package to solve my woes.

It appears, though, that PreferenceFragment isn't in the compatibility package. Can anyone tell me whether this was intentional? If so, can I easily target a range of devices (i.e. < 3.0 and >=3.0) or will I have to jump through hoops? If it wasn't intentionally excluded, can we expect a new release of the compatibility package? Or is there another workaround that is safe to use?

Cheers

James

Answer

CommonsWare picture CommonsWare · Mar 31, 2011

Discovering that PreferenceActivity contains deprecated methods (although these are used in the accompanying sample code)

The deprecated methods are deprecated as of Android 3.0. They are perfectly fine on all versions of Android, but the direction is to use PreferenceFragment on Android 3.0 and higher.

Can anyone tell me whether this was intentional?

My guess is it's a question of engineering time, but that's just a guess.

If so, can I easily target a range of devices (i.e. < 3.0 and >=3.0) or will I have to jump through hoops?

I consider it to be done "easily". Have two separate PreferenceActivity implementations, one using preference headers and PreferenceFragments, the other using the original approach. Choose the right one at the point you need to (e.g., when the user clicks on the options menu item). Here is a sample project demonstrating this. Or, have a single PreferenceActivity that handles both cases, as in this sample project.

If it wasn't intentionally excluded, can we expect a new release of the compatibility package?

You will find out when the rest of us find out, which is to say, if and when it ships.

Or is there another workaround that is safe to use?

See above.