SharedPreference刷新或清除不起作用

时间:2013-05-17 09:40:27

标签: android sharedpreferences

所以这是我的first.java

SharedPreferences preferences = getSharedPreferences("myPrefss", getApplicationContext().MODE_PRIVATE);
preferences.getString(currentPlace, "");
SharedPreferences.Editor editor = preferences.edit();
editor.putString("CurrentPlace", currentPlace);
editor.commit();

这是我的second.java

SharedPreferences preferences = getSharedPreferences("myPrefss",  getApplicationContext().MODE_PRIVATE);    
String curlocation;
curlocation = preferences.getString("CurrentPlace",""); 

说第一个类给出输出为aaa第一次现在当我回去重做过程我得到aaaaaa这次,字符串被追加。任何想法我怎么能避免这个?

2 个答案:

答案 0 :(得分:1)

preferences.clear()

之后使用SharedPreferences.Editor editor = preferences.edit();
 SharedPreferences preferences = getSharedPreferences("myPrefss", getApplicationContext().MODE_PRIVATE);
 SharedPreferences.Editor editor = preferences.edit();
 editor.clear();
 editor.putString("CurrentPlace", currentPlace);
 editor.commit();

答案 1 :(得分:0)

尝试使用

SharedPreferences preferences = getSharedPreferences("myPrefss", getApplicationContext().MODE_PRIVATE);
//preferences.getString(currentPlace, "");    // Remove this line
SharedPreferences.Editor editor = preferences.edit();
editor.putString("CurrentPlace", currentPlace);
editor.commit();