如何在Android上的片段中使用共享首选项?

时间:2014-02-12 06:13:54

标签: android facebook android-fragments sharedpreferences

我有一个片段,我想将Facebook ID存储在共享首选项中。我无法在get preference函数中写入private模式。而且我还希望在另一个片段中访问此共享首选项。我怎么能这样做?

这是我的代码......

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

6 个答案:

答案 0 :(得分:13)

Shared Preferences内使用Fragment;见下文。

首先写入SharedPreferences

SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putString("facebook_id", id);
edt.commit();

此处id是包含您已获得的Facebook ID的字符串,0表示private_mode。

其次,阅读SharedPreference中存储在另一个Fragment中的Facebook ID:

SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
String id = pref.getString("facebook_id", "empty");

如果facebook_id在SharedPreference内为空,则返回null为默认值。

答案 1 :(得分:3)

您也可以这样做,

editor = getActivity().getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
                        editor.putString("yourtextvalueKey", test);
                        editor.commit();

并获得

prefs = getActivity().getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
        text = prefs.getString("yourtextvalueKey", null);

答案 2 :(得分:2)

  • 创建一个单独的会话类:

    public class Session {
    private static String PREF_NAME = "Memory";
    private static String FBID = "FBID ";
    public static boolean saveSessionId(String FBID , Context context) {
    Editor editor = context.getSharedPreferences(PREF_NAME, 0).edit();
    editor.putString(FBID , FBID);
    return editor.commit();
    }
    
    public static String getSessionId(Context context) {
    SharedPreferences savedSession = context.getSharedPreferences(
            PREF_NAME, 0);
    return savedSession.getString(FBID , null);
    }
    }
    

要保存ID时,请调用save方法。然后,以同样的方式,调用getsessionid方法获取该ID。

答案 3 :(得分:2)

按如下方式更新SavePreferences方法:

private static String MY_PREFS = "My_Preference";
private void SavePreferences(String key, String value) {
    SharedPreferences sharedPreferences = getActivity().getSharedPreferences(
            MY_PREFS, 0);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.apply();
}

答案 4 :(得分:1)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    getSharedPrefs();
}

private void getSharedPrefs() {
    sharedpreferences = getActivity().getSharedPreferences(
            Data.MY_PREFERENCES, Context.MODE_PRIVATE);

    // check for all to be loaded here
    boolean isValid = false;

    int posImage = 0;

    if (sharedpreferences.contains(Data.COLUMN_IMAGENUMBER)) {
        posImage = sharedpreferences.getInt(Data.COLUMN_IMAGENUMBER, 0);
        if (posImage >= 0 || posImage < Data.PICS.length) {
            isValid = true;
        }
    } else {
        isValid = false;
    }
}

@Override
public void onStop() {
    super.onStop();
    Editor editor = sharedpreferences.edit();
    editor.putInt(Data.COLUMN_IMAGENUMBER, cv.currentPuzzleImagePosition);
    editor.apply();
}

答案 5 :(得分:1)

尝试使用此代码。对于此Shared preference来说,这已经足够了,无论您是在活动中工作还是Fragment,它都有效。 Shared preference无法反映或无效,无论是活动还是Fragment

如需更多点击here