24

I'm trying to use SharedPreferences to save settings. But I can't seem to get the data to be shared between any of my activities. The code I'm using does manage to save settings but each activity seems to have it's own version of each variable.

So for example. I have an audio settings activity in which the user can give a value to a variable "musicVolume" which is saved. If I close the game and reload it, then the audio settings activity "remembers" the value. But if I try to load the value into any other activity it doesn't work. But, they can all load and save their own variables of the same name.

These are the methods I'm using to save the variables. There is a copy of each of these methods in each activity.**

As I say, they work, but they can only seem to read and write data for the individual activity in which they are located.

public void SavePreferences(String key, float value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.commit();
}

public void LoadPreferences() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        musicVolume = sharedPreferences.getFloat("musicVolume", (float)0.123);
        soundEffectsVolume = sharedPreferences
                        .getFloat("soundEffectsVolume", (float)0.123);
}

public void ClearPreferences() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
}

** I know there is a better way to do this but I'm a very novice oo programmer. I tried to follow the advice of another thread

Android Shared Preferences

but wherever I tried to put the lines

protected AppPreferences appPrefs;
appPrefs = new AppPreferences(getApplicationContext());

I get an error of one sort or another. But most importantly, reading the other comments on the thread, people are saying that SharedPreferences are automatically shared between activities within the same package anyway, which is how I thought they work.

Community
  • 1
  • 1
user922220
  • 921
  • 2
  • 7
  • 7
  • "An error of some sort or another". Don't post things like that. Please be more explicit. Tell us the error. Anyways, do you have created a class called AppPreferences? Because it's not an Android class. – tolgap Jul 31 '12 at 19:38
  • Did you find an answer ? – Mr_and_Mrs_D May 01 '13 at 16:07
  • the right answer is just below dude :: [here](https://stackoverflow.com/a/11747857/4846859) **by @Niraj Burde**, please mark it as the right one. i tried it – Biskrem Muhammad Dec 11 '17 at 17:52

5 Answers5

47

You are using getPreferences(MODE). Use getSharedPreferences("PREF_NAME", MODE) instead. This way you will provide a name to particular preference and then you can call it by its name (PREF_NAME here) from whatever the activity you want.

//------get sharedPreferences

SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

//-------get a value from them

pref.getString("NAME", "Android");

//--------modify the value

pref.edit().putString("NAME", "Simone").commit();

//--------reset preferences

pref.edit().clear().commit();
Niraj Burde
  • 736
  • 1
  • 7
  • 16
13

As was said, use getSharedPreferences(String name, int mode) rather than getPreferences (int mode). Specifically, if you are interested, the documentation for these two methods illustrates the difference. According to the Android documentation getPreferences(int) does the following:

Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Matt Shaw
  • 131
  • 2
9

If you aren't doing anything fancy with preferences I would just use the default way of accessing them. It seems to be your problem.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

Here is a great write with more detail, put you are doing everything pretty much correctly except for the getting your handle. How do I get the SharedPreferences from a PreferenceActivity in Android?

ALSO: Don't forget the new way is to call .apply() instead of .commit() This was in one on the #io2012 videos..

Community
  • 1
  • 1
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64
  • I didn't know this, so I googled: `to call .apply() instead of .commit()`, the difference is `apply()` does not return a boolean indicating whether the commit was successful, and thus it's faster due to asycn. – WSBT May 21 '14 at 01:49
2

You should use this

SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

Or

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
Singhak
  • 8,508
  • 2
  • 31
  • 34
  • I use context.getSharedPreferences() in MySharedPreferences class. but it dont work for clear sharedPreferences variable and exit from app. are u have a suggestion? – roghayeh hosseini May 13 '19 at 09:36
1

If you had the same problem as me, it's very simple. When you to go save the preferences, save it like this:

SharedPreferences sp = getSharedPrefenreces("Preference",MODE_PRIVATE);

And not:

 SharedPreferences sp = getSharedPrefenreces("Preference's name",Context.MODE_PRIVATE);

I know how so much is important the SharedPrefenrences in some cases.

Mina Shaker
  • 383
  • 3
  • 12