2

I am passing values between two activities and fetching the values like this:

Bundle extras = getIntent().getExtras();
    if (extras != null)

    {
        initialUrl = extras.getString("initialUrl");
        isFollow = extras.getString("isFollow");
    }


    if (isFollow == "true") {
        editUrl.setText(initialUrl);
        setUpWebView(initialUrl);
    } else if (isFollow == "false") {
        editUrl.setText("http://www.google.com");
        setUpWebView("http://www.google.com");
    }

the problem is I can see the values being retrieved in the debug window by adding watch to the variables but when the compiler enters the statement if(isFollow=="true"), the condition fails. The else case is also not dealt with. What else do i need to do to ensure that my if condition is satisfied properly?

Sultan Saadat
  • 2,268
  • 6
  • 28
  • 38
  • Firstly send boolean in bundle and also if you are sending text then compare using equals not using == – ingsaurabh Jun 21 '11 at 11:58
  • == tests for reference equality. .equals() tests for value equality. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Zar E Ahmer Jun 11 '14 at 10:12

4 Answers4

2

You should use

isFollow.equals("true")

in your statements.

bart
  • 2,378
  • 2
  • 19
  • 21
1

If String type of data is put in bundle then Try with the following code

String isFollow = null;    
Bundle extras = getIntent().getExtras();
    if (extras != null)

    {
        initialUrl = extras.getString("initialUrl");
        isFollow = extras.getString("isFollow");
    }
if (isFollow.equals("true")) {
        editUrl.setText(initialUrl);
        setUpWebView(initialUrl);
    } else if (isFollow.equals("false")) {
        editUrl.setText("http://www.google.com");
        setUpWebView("http://www.google.com");
    }

If Boolean type of data is put in bundle then Try with the following code

 boolean isFollow = null;    
    Bundle extras = getIntent().getExtras();
        if (extras != null)

        {
            initialUrl = extras.getString("initialUrl");
            isFollow = extras.getBoolean("isFollow");
        }
    if (isFollow) {
            editUrl.setText(initialUrl);
            setUpWebView(initialUrl);
        } else {
            editUrl.setText("http://www.google.com");
            setUpWebView("http://www.google.com");
        }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

I know this is a really late answer but it would be better to do this if you are passing a Boolean into the intent from the sender:

Boolean isFollow = extras.getBoolean("isFollow");

if(isFollow) { 
  //Do stuff
}
lahsrah
  • 9,013
  • 5
  • 37
  • 67
0

You need to test either isFollow.equals("true") or if it is a boolean and not a string, either isFollow == true or just plain isFollow

(note the no quotes on the second one)

yep
  • 94
  • 2