我試圖使用androids sharedpreferences,我記錄了一切,下面的代碼真的提交了字符串集。問題是,當我強制關閉應用程序並重新啓動時,settings.getStringSet返回一個空集。沒有任何錯誤消息。設置<String>在android sharedpreferences不保存強制關閉
我試過PreferenceManager.getDefaultSharedPreferences,但這對我也不起作用。
感謝您的時間。
public static final String PREFS_NAME = "MyPrefsFile";
private static final String FOLLOWED_ROUTES = "followedRoutes";
,後來被稱爲保存時:
public void onFollowClicked(View view){
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
Set<String> follows = settings.getStringSet(FOLLOWED_ROUTES, new HashSet<String>());
follows.add(routeId);
editor.putStringSet(FOLLOWED_ROUTES, follows);
editor.commit();
}
有什麼問題? – Blackbelt
你尊重生命週期嗎?您在哪種狀態下保存數據? –
在我強制退出之前每一次都保存完畢。 問題是,settings.getStringSet(...不會返回我提交給它的值 我也嘗試替換commit()apply() - 沒有成功 – Malin