如何强制 PreferenceActivity 处理我的 SharedPreferences?

发布于 2024-12-09 02:24:48 字数 473 浏览 0 评论 0原文

我使用 SharedPreferences 来存储应用程序中所有Activities 的数据。我可以这样访问它:

SharedPreferences mSharedPreferences = getSharedPreferences("MyPrefs", 0);

我已经实现了 PreferenceActivity 因此用户可以通过它更改值,但它碰巧不是将数据读/写到“MyPrefs”而是:

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

这对我来说有点意外。是否可以强制 PreferenceActivity 处理我的“MyPrefs”首选项?在单个应用程序中拥有多个首选项有什么意义?谢谢。

I'm using SharedPreferences to store my data across all Activities within my application. I get access to it like this:

SharedPreferences mSharedPreferences = getSharedPreferences("MyPrefs", 0);

I've implemented PreferenceActivity so users can change values through it but it happens it reads/writes data not to "MyPrefs" but to:

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Which is a bit unexpected for me. Is that possible to force PreferenceActivity to deal with my "MyPrefs" preferences? And what is the point to have several preferences within single application? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乜一 2024-12-16 02:24:48

这是可能的而且简单的。这对我有用

public class SettingsActivity extends PreferenceActivity {
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName("MyPrefs");
        //getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_WRITEABLE);
    addPreferencesFromResource(R.xml.preferences);
    }
}

It is possible and simple. This worked for me

public class SettingsActivity extends PreferenceActivity {
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName("MyPrefs");
        //getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_WRITEABLE);
    addPreferencesFromResource(R.xml.preferences);
    }
}
小嗷兮 2024-12-16 02:24:48

我建议在任何地方都使用 PreferenceManager.getDefaultSharedPreferences(context) ,这与首选项活动所使用的相同。但是,如果您需要坚持当前的设置,那么一个 hacky 解决方案(但我知道的唯一一个)是重写 getSharedPreferences 以返回您想要的内容。

@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
    return super.getSharedPreferences("MyPrefs", mode);
}

I'd recommend just using PreferenceManager.getDefaultSharedPreferences(context) everywhere, which is the same as what the preference activity is using. But if you need to stick with your current setup then a hacky solution (but the only one I know of) is to override getSharedPreferences to return what you want.

@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
    return super.getSharedPreferences("MyPrefs", mode);
}
萌︼了一个春 2024-12-16 02:24:48

我可以这样访问它

没关系,只要您不打算使用 PreferenceActivity 即可。

我已经实现了 PreferenceActivity,以便用户可以通过它更改值

哎呀。

另外,删除那里的 getApplicationContext() ,除非您有特定原因使用 Application 而不是活动/服务/其他内容。仅在必要时才使用 Application 对象,并且您知道为什么必须这样做。

是否可以强制 PreferenceActivity 处理我的“MyPrefs”首选项?

不轻易。除非您有特殊原因要创建自己的 SharedPreferences 文件,否则我会使用默认文件。

在单个应用程序中拥有多个首选项有什么意义?

您可能有一个可重用的库或组件想要在 SharedPreferences 中存储内容,并且可能有自己的文件,以免弄乱托管应用程序的任何首选项。话虽如此,通常不需要多个首选项文件。

I get access to it like this

That's fine, so long as you weren't planning on using PreferenceActivity.

I've implemented PreferenceActivity so users can change values through it

Oops.

Also, get rid of getApplicationContext() there, unless you have a specific reason for using the Application rather than the activity/service/whatever. Only use the Application object when you have to and you know why you have to.

Is that possible to force PreferenceActivity to deal with my "MyPrefs" preferences?

Not readily. Unless you have some specific reason to invent your own SharedPreferences file, I'd use the default one.

And what is the point to have several preferences within single application?

You might have a reusable library or component want to store stuff in SharedPreferences, and that might have its own file so as not to mess up any preferences from the hosting application. That being said, multiple preference files is not typically needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文