sharedPreferences将不会在活动之间共享 [英] sharedPreferences won't share between activities

查看:158
本文介绍了sharedPreferences将不会在活动之间共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SharedPreferences保存设置.但是我似乎无法在我的任何活动之间共享数据.我正在使用的代码确实可以保存设置,但是每个活动似乎都具有每个变量自己的版本.

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.

例如.我有一个音频设置活动,用户可以在其中为保存的变量"musicVolume"赋值.如果我关闭游戏并重新加载游戏,则音频设置活动会记住"该值.但是,如果我尝试将值加载到任何其他活动中,将无法正常工作.但是,他们都可以加载和保存自己的同名变量.

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共享首选项

但是无论我在哪里放置线

but wherever I tried to put the lines

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

我得到一种或另一种错误.但是最重​​要的是,阅读该线程上的其他注释时,人们说SharedPreferences无论如何都是在同一包内的活动之间自动共享的,这就是我认为的工作方式.

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.

推荐答案

您正在使用 getPreferences(MODE).改用 getSharedPreferences("PREF_NAME",MODE).这样,您将为特定的首选项提供一个名称,然后您可以在任何想要的活动中以其名称(此处为PREF_NAME)进行命名.

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

这篇关于sharedPreferences将不会在活动之间共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆