Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...