I just noticed that addPreferencesFromResource();
is deprecated in Android
now. I tried looking for what I should use now and the developers site still has it this way. Does anyone know what I should use instead?
Asked
Active
Viewed 359 times
-1

Ryan Sayles
- 3,389
- 11
- 56
- 79
-
http://stackoverflow.com/questions/6822319/what-to-use-instead-of-addpreferencesfromresource-in-a-preferenceactivity – VM4 Dec 12 '13 at 19:11
-
http://stackoverflow.com/questions/16374492/alternative-to-addpreferencesfromresource-as-its-deprecated – Naddy Dec 12 '13 at 19:12
2 Answers
1
PreferenceActivity is still fine, but you have to use a PreferenceFragment subclass to do it now. It looks something like this:
public class SetupActivity extends PreferenceActivity {
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences);
}
}
You then store the preferences in preferences.xml
like this:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="rest_server"
android:defaultValue="@string/default_rest_server"
android:summary="@string/rest_url_desc"
android:title="@string/rest_url_title" />
<EditTextPreference
android:key="base_url"
android:defaultValue="@string/default_base_url"
android:summary="@string/base_url_desc"
android:title="@string/base_url_title" />
</PreferenceScreen>

David S.
- 6,567
- 1
- 25
- 45