Question

I am creating a Handler but for some reason the if statement does not trigger. The Log is printing out the correct value right before the if statement.

mHandler = new Handler() { 
@Override public void handleMessage(Message msg) { 
    String s=(String)msg.obj;
    s = s.trim();
    Log.v("mHandler reply", s);
    if(s == "OK"){
       Dialog.dismiss();
    }
}

};

Here is the log

03-24 09:02:53.707: V/mHandler reply(7331): OK

Why is this not working?

Was it helpful?

Solution

Use equals() method instead of == operator for String comparison as follows...

if(s.equals("OK")){
   Dialog.dismiss();
}

To get more information check How do I compare strings in Java?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top