1

I'm using a checkbox on an activity to determine what List needs to have the object added to it.

Intent i = getIntent();
String intentFlag = i.getStringExtra("flag");

if(listFlag.isChecked() && intentFlag == "main"){
    newProduct.put("shoppingList", true);
    newProduct.put("mainList", true);
} else if (listFlag.isChecked() && intentFlag == "shoppingList"){
    newProduct.put("mainList", true);
    newProduct.put("shoppingList", true);
} else if (!listFlag.isChecked() && intentFlag == "main"){
    newProduct.put("mainList", true);
    newProduct.put("shoppingList", false);
} else if (!listFlag.isChecked() && intentFlag == "shoppingList"){
    newProduct.put("mainList", false);
    newProduct.put("shoppingList", true);
}

At the moment, the only intentFlag that can be returned from the Intent is "main", however, code within the IF statement is never reached. Looking at the Debug, it's returning "main"

Am I doing something obviously wrong?

OleGG
  • 8,589
  • 1
  • 28
  • 34
jmo
  • 357
  • 1
  • 4
  • 12

1 Answers1

4

In Java you should compare strings using equals(), not ==. So it should be:

if(listFlag.isChecked() && intentFlag.equals("main")){
   ....

or to avoid potential NullPointerException, safer form (in case intentFlag is null) would be:

if(listFlag.isChecked() && "main".equals(intentFlag)){
   ...
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I'm such an idiot. Been a long day. Thanks for your help, will accept as answer when it lets me – jmo Aug 12 '15 at 21:56