0

I am using the following logic, I have two buttons in my activity 1 button sets flag to F and the other to C, the SOP statement in the below code prints correct if the value of flag each if its F it prints F else it prints C. But the If else statements I dont know what goes wrong, In each of the if else state I am trying the setText for TextView depending upon the condition if its F or C but everytime C is being called. Dont know why.

   Bundle extras = getIntent().getExtras();
    if (extras != null) {
      flag = extras.getString("Flag");
      System.out.println(flag);
      if(flag == "F"){
        do F
      }
      else{
        do C
      }
    else{
      do F + C
    }
Hrishikesh Sardar
  • 2,907
  • 4
  • 21
  • 33

3 Answers3

1

A much cleaner way would be

boolean flag = flag.equalsIgnoreCase("f") : false : true

then use your flag here

as

if(flag)
{
 ....
}

Or even for simplicity just store a boolean in the extra instead of a string...

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

Strings are reference types. Since this is java, you would have to do:

if(flag.equals("F")){
do F
}
Ashwin Chandran
  • 1,447
  • 14
  • 11
0

Your problem is not about passing data between activities. The == is not comparing the value of your String, use equals() instead of ==.