Leos Literak
Leos Literak

Reputation: 9474

How to get sharedPreferences in Application

I have implemented a configuration that uses PreferenceFragment:

addPreferencesFromResource(R.xml.game_prefs);

Now I want to handle upgrade process, so I started to implement a procedure in android.app.Application subclass.

The issue is that I do not know how to get correct prefs instance.

There is no name in Fragment:

getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

But prefs name is mandatory here in App subclass:

getApplicationContext().getSharedPreferences(XX,mode)

Where can I get correct name?

Upvotes: 1

Views: 234

Answers (2)

cyroxis
cyroxis

Reputation: 3711

From the Settings developer guide

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String syncConnPref = sharedPref.getString(SettingsActivity.KEY_PREF_SYNC_CONN, "");

Upvotes: 1

Alexander N.
Alexander N.

Reputation: 1592

If you can access the activity, you can get it from getPreferences().

More information found here:

Saving Key-Value Sets http://developer.android.com/training/basics/data-storage/shared-preferences.html

getPreferences is used when you do not store it to a specific file name.

Upvotes: 0

Related Questions