Bill Mote
Bill Mote

Reputation: 12823

Shared Preferences not evaluating correctly?

Of course it's not 1-for-1 code correct, but the gist of my question can be gleaned from this. Why is the result of my if statement always true? I'm guessing it has something to do with the file encoding of the preferences file? I've tried adding .toString() to the end of both. I have dumped a Toast out to see that "2.4" and "2.4" is what is returned.

if (appPrefs.getAppVer() != getAppVerName()) {
   //TODO display Changes Pop-up
}

public String getAppVer() {
   return appSharedPrefs.getString("appVer", "");
}

public String getAppVerName() {
   return getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
}

Upvotes: 2

Views: 121

Answers (2)

Maaalte
Maaalte

Reputation: 6081

Always compare Strings with string1.equals(string2)

Upvotes: 1

Andrew White
Andrew White

Reputation: 53496

Because you need to use .equals and not != for string compares.

Upvotes: 3

Related Questions