문제

I am using following code:

SharedPreferences sharedPref = getSharedPreferences(GlobalDefines.SHARED_PREFERENCES, Context.MODE_PRIVATE);
String test = sharedPref.getString(GlobalDefines.GCM_KEY, "");        
SharedPreferences.Editor editor = sharedPref.edit(); 
editor.clear();
editor.putBoolean(GlobalDefines.USER_IS_LOGGED_IN, false);
editor.remove(GlobalDefines.USER_NAME);
editor.remove(GlobalDefines.USER_PASSWORD);
editor.commit();
test = sharedPref.getString(GlobalDefines.GCM_KEY, "");

The string "test" has a value when I get the value from the shared preferences for the first time; when I remove another value from the preferences and want to get the same value (GCM_KEY) again, it is returned empty.

Why is that?

도움이 되었습니까?

해결책

editor.clear() tells the editor that you want to remove ALL values from your SharedPreferences. Remove this line and you will see the expected behavior.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top