I'm using Shared Preferences to access user settings and the onSharedPreferenceChanged function to handle changes that need to be made in my app as preferences are changed. It resides in my Preferences class (which implements OnSharedPreferenceChangeListener). I ran into a very weird issue with this function though.
For some reason, if statements don't seem to run inside the function!!! For example, the first line of code in the function below properly displays a toast message that simply says "myKey" with no spaces before or after it. The second line of code should compare the key passed to the function against "myKey" (which results as true) and run the corresponding toast message. However, this never happens!!! I really need to run if statements in here.
I've been staring at this piece of code all night, maybe I'm missing something simple and need a fresh set of eyes. Anyone have any idea why this would happen?
UPDATE:
Upon further testing I found that the following works:
Which tells me that if statements themselves are working, but it isn't equating key to be equal to "myKey" as it should. Any ideas would be greatly appreciated!!!
For some reason, if statements don't seem to run inside the function!!! For example, the first line of code in the function below properly displays a toast message that simply says "myKey" with no spaces before or after it. The second line of code should compare the key passed to the function against "myKey" (which results as true) and run the corresponding toast message. However, this never happens!!! I really need to run if statements in here.
Code:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
Toast.makeText(this, key, 2000).show(); // displays "myKey" (without quotes)
if (key == "myKey") {Toast.makeText(this, "It Worked!!!", 2000).show();} //does nothing!!!
}
I've been staring at this piece of code all night, maybe I'm missing something simple and need a fresh set of eyes. Anyone have any idea why this would happen?
UPDATE:
Upon further testing I found that the following works:
Code:
if ([B][COLOR="Red"]key == key[/COLOR][/B]) {Toast.makeText(this, "It Worked!!!", 2000).show();}
Which tells me that if statements themselves are working, but it isn't equating key to be equal to "myKey" as it should. Any ideas would be greatly appreciated!!!