2014-02-12 88 views
4

我有一個片段,我想存儲Facebook的共享偏好。我無法在獲取首選項功能中編寫私有模式。而且我想在另一個片段中訪問這個共享偏好。我該怎麼做?如何在Android上的片段中使用共享首選項?

這裏是我的代碼...

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

嘗試'Context.MODE_PRIVATE'代替。 –

+0

使用preferencemanager.getsharedpreference(上下文)... – Ranjit

+0

更正您的代碼的格式 - (提示:沒有水平滾動條等) –

回答

14

使用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。

其次,讀取另一個Fragment存儲在SharedPreference Facebook的ID:

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

空的。這裏是如果facebook_id裏面SharedPreference空返回的默認值。

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保存方法。然後,以相同的方式,調用getsessionid方法來獲得該ID。

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

lint會告訴你考慮使用apply – user896033

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); 
1

與此代碼嘗試。這對於此Shared preference就足夠了,它可以用於活動或FragmentShared preference無法反映或無法生效活動或Fragment

詳細點擊here