I'm writing some Android code that downloads the application EULA as a String and stores it in the SharedPreferences so we can check it later. The general idea is that the user should only have to agree to the EULA once, but if the terms are updated we want to notify the user and have them agree again.
Here is some example code, which takes place within an AsyncTask's doInBackground method:
String currentEula = getEulaText(); //I've confirmed this method correctly downloads the text
String oldEula = mPrefs.getString("eula_text", "");
if(!(oldEula.equals(currentEula)){
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("eula_text", currentEula);
editor.commit();
runOnUiThread(new Runnable {
@Override
public void run() {
showEula() //this handles the actual agreement stuff, works correctly
}
}
}
However, currentEula and oldEula are never equal in this example, so every time I open the app the EULA gets displayed. When I printed the length of both Strings, I found that the String coming from mPrefs is 4 characters longer than the one that gets downloaded. I added a log statement to print the 4 extra characters, and they are blank. If I compare the remainder of the Strings, they are equal.
To make matters even stranger, after calling editor.commit(), this comparison is true:
mPrefs.getString("eula_text", "").equals(currentEula);
So the value of the String retrieved from mPrefs changes between the end of this function and the time when it gets called again, but I've confirmed that no other part of the application touches the "eula_text" value (and even if it did, why on Earth would it just add four blank spaces to the end?).
I could just strip the last 4 characters from the String after retrieving it from mPrefs, but I'm worried they may not always be blank. Alternately, I could do this comparison:
oldEula.substring(0, currentEula.length()).equals(currentEula)
However, this yields a false positive in the case where a EULA update simply strips the last item (not super likely, but still possible). Does anyone know why my String would randomly have 4 blank characters tacked onto it?