所以这是我的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这次,字符串被追加。任何想法我怎么能避免这个?
答案 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();