Fragment 中的 Android SharedPreferences
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11741270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android SharedPreferences in Fragment
提问by Mark
I am trying to read SharedPreferences inside Fragment. My code is what I use to get preferences in any other Activity.
我正在尝试读取 Fragment 中的 SharedPreferences。我的代码是我用来在任何其他活动中获取首选项的代码。
SharedPreferences preferences = getSharedPreferences("pref", 0);
I get error
我得到错误
Cannot make a static reference to the non-static method getSharedPreferences(String, int) from the type ContextWrapper
I have tried to follow these links but with no luck Accessing SharedPreferences through static methodsand Static SharedPreferences. Thank you for any solution.
我试图遵循这些链接,但没有成功通过静态方法和 静态 SharedPreferences访问SharedPreferences。感谢您提供任何解决方案。
回答by Jug6ernaut
The method getSharedPreferences
is a method of the Context
object, so just calling getSharedPreferences from a Fragment
will not work...because it is not a Context! (Activity is an extension of Context, so we can call getSharedPreferences from it).
该方法getSharedPreferences
是Context
对象的方法,因此仅从 a 中调用 getSharedPreferences 是Fragment
行不通的...因为它不是 Context!(Activity 是 Context 的扩展,因此我们可以从中调用 getSharedPreferences)。
So you have to get your applications Context by
所以你必须得到你的应用程序上下文
// this = your fragment
SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
回答by Leon
The marked answer didn't work for me, I had to use
标记的答案对我不起作用,我不得不使用
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
EDIT:
编辑:
Or just try removing the this
:
或者只是尝试删除this
:
SharedPreferences prefs = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
回答by ed209
As a note of caution this answer provided by the user above me is correct.
请注意,我上面的用户提供的这个答案是正确的。
SharedPreferences preferences = this.getActivity().getSharedPreferences("pref",0);
However, if you attempt to get anything in the fragment before onAttach is called getActivity() will return null.
但是,如果您尝试在 onAttach 调用之前获取片段中的任何内容,则 getActivity() 将返回 null。
回答by misbahm3
You can make the SharedPrefences
in onAttach
method of fragment like this:
您可以像这样制作片段的SharedPrefences
inonAttach
方法:
@Override
public void onAttach(Context context) {
super.onAttach(context);
SharedPreferences preferences = context.getSharedPreferences("pref", 0);
}
回答by Abdeljabar Taoufikallah
This did the trick for me
这对我有用
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
Check here https://developer.android.com/guide/topics/ui/settings.html#ReadingPrefs
在这里查看https://developer.android.com/guide/topics/ui/settings.html#ReadingPrefs
回答by Kirguduck
getActivity()
and onAttach()
didnot help me in same situation
maybe I did something wrong
but! I found another decision
I have created a field Context thisContext
inside my Fragment
And got a current context from method onCreateView
and now I can work with shared pref from fragment
getActivity()
并onAttach()
因此未帮助我在相同的情况下
,也许我做错了什么事
,但!我发现了另一个决定,
我Context thisContext
在 Fragment 中创建了一个字段,
并从 onCreateView 方法中获取了当前上下文
,现在我可以使用Fragment 中的共享首选项
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
thisContext = container.getContext();
...
}
回答by user11809346
To define the preference in Fragment:SharedPreferences pref = getActivity().getSharedPreferences("CargaDatosCR",Context.MODE_PRIVATE);
editor.putString("credi_credito",cre);
editor.commit();
在 Fragment 中定义首选项:SharedPreferences pref = getActivity().getSharedPreferences("CargaDatosCR",Context.MODE_PRIVATE);
editor.putString("credi_credito",cre);
editor.commit();
To call another activity or fragment the preference data:SharedPreferences pref = getActivity().getSharedPreferences("CargaDatosCR", Context.MODE_PRIVATE);
credit=pref.getString("credi_credito","");
if(credit.isNotEmpty)...
要调用另一个活动或将首选项数据分段:SharedPreferences pref = getActivity().getSharedPreferences("CargaDatosCR", Context.MODE_PRIVATE);
credit=pref.getString("credi_credito","");
if(credit.isNotEmpty)...