问题描述
我有一个片段,我想Facebook的ID存储在共享preference。我不能写模式的get preference功能专用。同时,我想在另一个片段访问此共享preference。我该怎么办呢?
下面是我的code ...
Session.openActiveSession(getActivity(),真,新Session.StatusCallback()
{
@覆盖
公共无效呼叫(会话的会话,
SessionState会的状态,
例外的例外){ 如果(session.isOpened()){
Request.executeMeRequestAsync(会话,新Request.GraphUserCallback(){ @覆盖
公共无效onCompleted(GraphUser用户,响应响应){ 如果(用户!= NULL){
T =(TextView中)rootView.findViewById(R.id.textView2);
P =(ProfilePictureView)rootView.findViewById(R.id.profilePictureView1);
p.setProfileId(user.getId());
S = user.getName();
t.setText(多个);
S1 = user.getId(); 私人无效节省preferences(字符串键,字符串值)
{
共享preferences共享preferences = GET preferences(MODE_PRIVATE);
共享preferences.Editor编辑=共享preferences.edit();
editor.putString(键,值);
editor.commit();
}
使用共享preferences
在片段
;请看下文。
首先写共享preferences
:
共享preferences preF = getActivity()获得preferences(0);
共享preferences.Editor EDT = pref.edit();
edt.putString(facebook_id,ID);
edt.commit();
下面id是包含Facebook的ID,你已经得到了字符串,0表示private_mode。
二,读取存储在另一个共享preference
片段
Facebook的ID:
共享preferences preF = getActivity()获得preferences(0);
字符串ID = pref.getString(facebook_id,空);
下面是空的默认值返回,如果facebook_id为null里面共享preference
。
I have a fragment and I want to store the Facebook id in a shared preference. I can't write mode private in the get preference function. And also I want to access this shared preference in another fragment. How can I do so?
Here is my code...
Session.openActiveSession(getActivity(), true, new Session.StatusCallback()
{
@Override
public void call(Session session,
SessionState state,
Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
t = (TextView)rootView.findViewById(R.id.textView2);
p = (ProfilePictureView)rootView.findViewById(R.id.profilePictureView1);
p.setProfileId(user.getId());
s = user.getName();
t.setText(s);
s1 = user.getId();
private void SavePreferences(String key,String value)
{
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
Use Shared Preferences
inside a Fragment
; see below.
First write in SharedPreferences
:
SharedPreferences pref = getActivity().getPreferences(0);
SharedPreferences.Editor edt = pref.edit();
edt.putString("facebook_id", id);
edt.commit();
Here id is the string containing the Facebook id which you've got, and 0 indicates private_mode.
Second, to read the Facebook id stored in SharedPreference
in another Fragment
:
SharedPreferences pref = getActivity().getPreferences(0);
String id = pref.getString("facebook_id", "empty");
Here empty is the default value returned if facebook_id is null inside SharedPreference
.
这篇关于我如何使用共享preferences在Android上的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!