0

所以这是我的 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,字符串被附加。知道如何避免这种情况吗?

4

2 回答 2

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();
于 2013-05-17T09:47:33.223 回答
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();
于 2013-05-17T09:56:58.587 回答