What to Use Instead of "Addpreferencesfromresource" in a Preferenceactivity

What to use instead of addPreferencesFromResource in a PreferenceActivity?

No alternative method is provided in the method's description because the preferred approach (as of API level 11) is to instantiate PreferenceFragment objects to load your preferences from a resource file. See the sample code here: PreferenceActivity

Alternative to addPreferencesFromResource as its deprecated

PreferenceActivity is not deprecated.

addPreferencesFromResource() on PreferenceActivity is deprecated. However, if you are supporting API Level 10 or lower, you have no choice but to use it, at least on those devices.

The non-deprecated approach is to use PreferenceFragment in conjunction with PreferenceActivity, as is described in the PrefereceActivity documentation. If your app is only supporting API Level 11 and higher, just use that. If your app is supporting older devices, either:

  • Use addPreferencesFromResource() all the time, as it still works, until you drop support for the older versions, or

  • Use addPreferencesFromResource() only on the older devices (by checking Build.VERSION.SDK_INT), and rely on the new fragment-based system on newer devices

The method addPreferencesFromResource(int) from the type PreferenceActivity is deprecated

Use PreferenceFragment instead. You may face some troubles in the future, right now PreferenceActivity works fine.

How to use preferencesResId instead addPreferencesFromResource

preferencesResId() is not a method, it is a placeholder in conversation for your resource id (R.xml.settings).

So although

addPreferencesFromResource(R.xml.settings);

Is deprecated, if you're using this approach, this is still the most correct way - there isn't another way using the deprecated approach.

I suggest you look at this SO question - it tells you what you should be using instead (PreferenceFragments). If you need a code sample, fire up Eclipse and the ADT plugin and make a SettingsActivity via the new Activity Wizard.

And as usual, here is the full JavaDoc for PreferenceActivity.

PreferenceActivity Android 4.0 and earlier

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.



Related Topics



Leave a reply



Submit