0

I have an app on a startup that performs 3 checks (everything happens in a separate file called "Application"): first for an internet connection, then for the internet connection type, and lastly, for the apps permission to start main activity.

All 3 checks pass correctly, however, on the last pass I get in a string value "False", which is fine, but in the splash screen when I want to check that value, it does not work. I did set if block if the value is false to launch another acitivty, but it does not, it just ignores that block.

Here is the code:

 public void onCreate(Bundle savedInstanceState) {
    setFullScreen();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_activity_init);
    if(APP.connectionCheck(InitActivity.this) == "TRUE"){
        JSONObject JOBJC = APP.getJSON(APP.defaultUrl());
        String result = APP.checkPermission(JOBJC);
        if(result=="false"){
            Intent i = new Intent(this,app.pcg.notation.Notation.class);
            startActivity(i);
            finish();
        }else if(result=="true"){

        }
    }else{
        Intent i = new Intent(this,app.pcg.notation.Notation.class);
        startActivity(i);
        finish();
    }
}
vba_user111
  • 215
  • 1
  • 15
Silvio Marijic
  • 483
  • 4
  • 12
  • 22
  • `==` compares Strings Refrences(Memory Location) AND `.equals()` compare String Value. http://stackoverflow.com/questions/767372/java-string-equals-versus – Samir Mangroliya Sep 29 '12 at 13:19
  • 1
    The real question is: why are you returning a `String` from your methods when it seems as if a `boolean` would be more appropiate? – reima Sep 29 '12 at 13:21
  • well , what difference does it make ? – Silvio Marijic Sep 29 '12 at 13:24
  • `boolean` can only assume the values `true` or `false`, which seems to be exactly what you need. `String` on the other hand is just a series of characters, open for interpretation. Using `String` in this context is like using a text input field in a "yes/no" question in a web form instead of a checkbox. – reima Sep 29 '12 at 13:28

4 Answers4

1

Change to this result.equals("false") or result.equalsIgnoreCase("false") . You can not compare string using == Operator.

Do it same for else part .

result.equals("true") or result.equalsIgnoreCase("true")

Edit Code

if(APP.connectionCheck(InitActivity.this).equalsIgnoreCase("true")){
        JSONObject JOBJC = APP.getJSON(APP.defaultUrl());
        String result = APP.checkPermission(JOBJC);
        if(result.equalsIgnoreCase("false")){
            Intent i = new Intent(this,app.pcg.notation.Notation.class);
            startActivity(i);
            finish();
        }else if(result.equalsIgnoreCase("true")){

        }
    }else{
        Intent i = new Intent(this,app.pcg.notation.Notation.class);
        startActivity(i);
        finish();
    }
Chirag
  • 56,621
  • 29
  • 151
  • 198
1

You cannot compare Strings with == in Java. Use String.equals() instead.

See How do I compare strings in Java? for a good explanation.

Community
  • 1
  • 1
reima
  • 2,076
  • 15
  • 22
1

String comparisons MUST be done with .equals(). Using == for objects compares the pointer value (i.e. is it the same object?) rather than the value.

So, if(APP.connectionCheck(InitActivity.this).equals("TRUE")) ...

dmon
  • 30,048
  • 8
  • 87
  • 96
1

always use equals(); while you are comparing two strings

because == operator checks reference to the objects, while equals() checks its value.

you will get proper result

if( APP.connectionCheck(InitActivity.this).equals("true"))
{
}

or

if( APP.connectionCheck(InitActivity.this).equalsIgnoreCase("true"))
{
}
MAC
  • 15,799
  • 8
  • 54
  • 95