SharedPreferences
PreferenceActivity
Android development
Android preferences
mobile app development

How do I get the SharedPreferences from a PreferenceActivity in Android?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In classic Android preference screens, PreferenceActivity usually writes values into the default shared-preferences file managed by the preference framework. That means the usual answer is not to search for a hidden special object, but to access the same SharedPreferences file that the preference screen is already using.

In older code that often means PreferenceManager.getDefaultSharedPreferences(...). In newer apps, PreferenceActivity itself is legacy and PreferenceFragmentCompat is preferred, but the underlying preference-storage idea is the same.

Get The Default Preferences

If the preference screen is using the default preference manager, this is the standard access pattern:

java
1import android.content.SharedPreferences;
2import android.os.Bundle;
3import android.preference.PreferenceActivity;
4import android.preference.PreferenceManager;
5
6public class SettingsActivity extends PreferenceActivity {
7    @Override
8    protected void onCreate(Bundle savedInstanceState) {
9        super.onCreate(savedInstanceState);
10
11        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
12        boolean enabled = prefs.getBoolean("notifications_enabled", false);
13    }
14}

That works because the preference widgets and your manual reads are pointing at the same default shared-preferences file.

Access Through The Preference Manager

Inside a PreferenceActivity, you may also see this style:

java
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();

This is useful when you want the specific SharedPreferences instance tied to that activity's preference manager configuration.

In practice, both styles often lead to the same file when the activity uses the default preference storage.

Listen For Preference Changes

If you need to react when a setting changes, register a listener:

java
1public class SettingsActivity extends PreferenceActivity
2        implements SharedPreferences.OnSharedPreferenceChangeListener {
3
4    private SharedPreferences prefs;
5
6    @Override
7    protected void onResume() {
8        super.onResume();
9        prefs = PreferenceManager.getDefaultSharedPreferences(this);
10        prefs.registerOnSharedPreferenceChangeListener(this);
11    }
12
13    @Override
14    protected void onPause() {
15        prefs.unregisterOnSharedPreferenceChangeListener(this);
16        super.onPause();
17    }
18
19    @Override
20    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
21        // react to changed preference
22    }
23}

This is often cleaner than reading values manually after every UI interaction.

Modern Android Note

PreferenceActivity is a legacy API. In current Android development, preference screens are usually implemented with AndroidX PreferenceFragmentCompat. The storage model still uses SharedPreferences by default, but newer code should follow the fragment-based approach rather than building new screens on top of PreferenceActivity unless you are maintaining an older app.

Default File Versus Named File

Most examples use the default shared-preferences file because that is what the preference framework uses unless configured otherwise. If the app deliberately points preferences at a custom file name, your manual reads must use that same file or the values will appear to be missing.

Reading Values Is Type-Specific

Once you have the preferences object, use the getter that matches the stored type, such as getBoolean, getString, or getInt. A wrong default type or wrong getter often looks like a missing preference even when the key exists.

Legacy API, Familiar Storage

Even though PreferenceActivity is old, the stored preference values are still just shared-preferences entries underneath.

That is why the preference keys remain the real source of truth.

Common Pitfalls

  • Reading from a different preferences file than the one the preference screen actually uses.
  • Assuming PreferenceActivity stores values somewhere other than SharedPreferences by default.
  • Forgetting default values when reading with getBoolean, getString, or similar methods.
  • Registering a preference-change listener and never unregistering it.
  • Starting new code with PreferenceActivity instead of a modern preference fragment.

Summary

  • In a PreferenceActivity, use the same SharedPreferences file that the preference framework uses.
  • 'PreferenceManager.getDefaultSharedPreferences(this) is the common answer.'
  • 'getPreferenceManager().getSharedPreferences() is also valid inside the activity.'
  • Register a listener if you need to react to changes immediately.
  • For new Android code, prefer PreferenceFragmentCompat, even though the storage idea remains similar.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.