如何强制preferenceActivity对付我的共享preferences? [英] How to force PreferenceActivity to deal with my SharedPreferences?

查看:103
本文介绍了如何强制preferenceActivity对付我的共享preferences?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用共享preferences 来存储所有活动在我的应用我的数据。我访问它是这样的:

 共享preferences mShared preferences = getShared preferences(我的preFS,0);
 

我实现了 preferenceActivity ,以便用户可以通过改变它的值,但它发生在读取/写入数据不为我的preFS,而是要

  preferenceManager.getDefaultShared preferences(getApplicationContext());
 

这是一个有点意外的我。这有可能迫使 preferenceActivity 来处理我的我的preFSpreferences?什么是点有内单个应用程序数preferences?谢谢你。

解决方案
  

我可以访问它像这样

这很好,只要你不打算使用 preferenceActivity

  

我实现了preferenceActivity,使用户可以通过它更改值

抱歉。

此外,摆脱 getApplicationContext的()在那里,除非你有特殊原因使用应用程序而比活性/服务/不管。仅使用应用程序对象时,你必须和你知道为什么你要。

  

这有可能迫使preferenceActivity对付我的我的preFSpreferences?

不容易。除非你有特殊原因,发明自己的共享preferences 的文件,我会使用默认的。

  

什么是点有内单个应用程序数preferences?

您可能有一个可重复使用的库或组件要存储的东西,在共享preferences ,并可能有它自己的文件,以免弄乱任何preferences从宿主应用程序。话虽这么说,是不是通常需要多个preference文件。

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.

解决方案

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.

这篇关于如何强制preferenceActivity对付我的共享preferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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