Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharedPreferences won't share between activities

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.

like image 369
user922220 Avatar asked Jul 31 '12 19:07

user922220


People also ask

Is SQLite better than SharedPreferences?

To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through.

What is the difference between commit and apply in shared Prefs?

Use apply(). It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.

What is difference between preferences and SharedPreferences?

Preferences: The user interfaces part of the settings. It contains different classes which allow one to compose Settings screens from code or XML. Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you.


4 Answers

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();
like image 174
Niraj Burde Avatar answered Dec 23 '22 05:12

Niraj Burde


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.

like image 31
Matt Shaw Avatar answered Dec 23 '22 04:12

Matt Shaw


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..

like image 33
Frank Sposaro Avatar answered Dec 23 '22 05:12

Frank Sposaro


You should use this

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

Or

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
like image 34
Singhak Avatar answered Dec 23 '22 05:12

Singhak