What to use instead of addPreferencesFromResource in a PreferenceActivity?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
addPreferencesFromResource(...) belonged to the older PreferenceActivity model and is not the modern answer for Android settings screens. Today, the preferred replacement is PreferenceFragmentCompat from AndroidX, using setPreferencesFromResource(...) inside the fragment.
Move from PreferenceActivity to PreferenceFragmentCompat
The old pattern loaded preferences directly inside an activity. Modern Android apps use a fragment-based approach instead because it fits better with current lifecycle, navigation, and app architecture patterns.
A typical replacement looks like this:
This is the direct conceptual replacement for the old API.
Why the New Pattern Is Better
Using PreferenceFragmentCompat gives you:
- AndroidX support
- fragment lifecycle integration
- easier navigation and back-stack behavior
- compatibility with modern theme and toolbar setups
It also makes settings screens easier to compose. Instead of one giant activity-centric settings class, you can separate settings into fragments by category when the app grows.
Keep the XML Preference Resource
The XML itself usually stays familiar. You still define preferences in a resource file such as res/xml/preferences.xml.
The main change is not the preference XML. The main change is where and how it gets attached to the UI.
Add the AndroidX Preference Dependency
If the project does not already use AndroidX preferences, add the dependency first.
Once that is present, PreferenceFragmentCompat and the newer preference widgets are available.
Host the Fragment Like a Normal Modern Screen
Another advantage of the fragment-based approach is that settings can now live inside a normal AppCompatActivity with a toolbar, navigation support, and the same theming approach as the rest of the app. That makes migration easier because you are no longer treating the settings screen as a special legacy island with a separate UI model.
If the app uses Navigation Component, the settings fragment can also participate in the existing navigation graph instead of being launched through old PreferenceActivity patterns.
That integration becomes especially useful once settings screens need deep links, nested navigation, or separate preference categories.
Handle Preference Changes the Modern Way
You can still look up preferences and listen for changes inside the fragment or through SharedPreferences.
That behavior is familiar, but the fragment-based structure keeps it aligned with the rest of a modern Android app.
Common Pitfalls
- Replacing
addPreferencesFromResourcewith newer widgets while still keeping the old activity-centric mental model. - Forgetting to add the AndroidX preference dependency before using
PreferenceFragmentCompat. - Mixing legacy preference classes with AndroidX classes in the same screen.
- Assuming the XML preference file itself must be rewritten completely when only the hosting pattern changed.
- Registering preference listeners without respecting the fragment lifecycle.
Summary
- The modern replacement for
addPreferencesFromResource(...)isPreferenceFragmentCompatplussetPreferencesFromResource(...). - Settings screens should now be fragment-based, not built directly around
PreferenceActivity. - Preference XML files are still used, but they are loaded inside the fragment lifecycle.
- Add the AndroidX preference dependency before migrating.
- This pattern fits modern Android navigation, theming, and lifecycle management much better than the deprecated approach.

