-1

I am a noob to android development and I am trying to use shared preferences to get values for a double during onCreate. However, my code for doing this seems to only work the first time I restart my app. During, subsequent restarts the code doesn't work and skips over the if/else portion of my code. This is very frustrating because if it works one time it should work every time. Any help is greatly appreciated.

public void LoadPreferences(){
                            double portfoliocurrency1 = null;
            SharedPreferences portfoliopreferences = getPreferences(MODE_PRIVATE);
            String isuser1created = portfoliopreferences.getString("U1C", "");
            if(isuser1created.equals("yes")){           
                String savedportfolioname = portfoliopreferences.getString("PN1", "");
                String saveddenomination = portfoliopreferences.getString("DN1", "");
                String savedporfoliocurrency = portfoliopreferences.getString("PC1", ""); //<--Always either "usd", "inr", or "eur".
                Log.d("test","SPConLoad=" + savedporfoliocurrency ); //<--Returns correct value on every restart.
                Log.d("test","USD=" + currencyUSD ); //<--Returns correct value on every restart.
                Log.d("test","EUR=" + currencyEUR ); //<--Returns correct value on every restart.
                pn1 = savedportfolioname;
                denomination1 = saveddenomination;
                if(savedporfoliocurrency=="usd"){ //<--If/else statement only works on first restart.  For some reason it is skipped on subsequent restarts.
                    portfoliocurrency1 = currencyUSD; 
                    Log.d("test","PC1USD=" + currencyUSD ); //<--Returns correct value on first restart.
                }else if(savedporfoliocurrency=="eur"){
                    portfoliocurrency1 = currencyEUR;
                    Log.d("test","PC1EUR=" + currencyEUR ); //<--Returns correct value on first restart.
                }else if(savedporfoliocurrency=="inr"){
                    portfoliocurrency1 = currencyINR; //<--Returns correct value on first restart.
                }
                Log.d("test","PC1onLoad=" + portfoliocurrency1 ); //<-- Only returns correct value during first restart.  Always returns null on subsequent restarts.
              }
B. Money
  • 931
  • 2
  • 19
  • 56

2 Answers2

3

You should be using String.equals() here, not ==.

if(savedporfoliocurrency=="usd"){

See, for example, the SO question Java String.equals versus ==.

==, as you found, will work sometimes, but it's not what you want to do here. You want to check the contents, not the location where they're stored.

Community
  • 1
  • 1
Hew Wolff
  • 1,489
  • 8
  • 17
  • Thanks. Just tried it and it works like a charm. I had spent the last hour trying to figure out what was wrong. Thanks again. – B. Money Oct 22 '12 at 22:59
1

Change:

savedporfoliocurrency=="usd"

to

savedporfoliocurrency.equals("usd")

and in similar manar fix other string compares

marcinj
  • 48,511
  • 9
  • 79
  • 100