我试图使用androids共享偏好,我记录了所有内容,下面的代码实际上提交了字符串集.问题是当我强制关闭应用程序并再次启动时,settings.getStringSet返回一个空集.没有任何错误消息.
我曾尝试过PreferenceManager.getDefaultSharedPreferences,但这对我也不起作用.
谢谢你的时间.
public static final String PREFS_NAME = "MyPrefsFile";
private static final String FOLLOWED_ROUTES = "followedRoutes";
Run Code Online (Sandbox Code Playgroud)
然后在保存时调用:
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();
}
Run Code Online (Sandbox Code Playgroud)