-6

I have a piece of code here that theoretically should work, but in fact it doesn't.

        Extras = getIntent().getExtras();
    if(Extras!=null)
    if(Extras.getString("startup")!=null)
        StartUp = Extras.getString("startup");
    else
        StartUp = "0";

    prefs = this.getSharedPreferences("runonstartup", 0);
    start.setChecked(prefs.getBoolean("yesorno", false));

    Toast.makeText(getApplicationContext(), "Start-up: "+StartUp+"\nChecked: "+String.valueOf(start.isChecked()), Toast.LENGTH_LONG).show();

    if(StartUp=="1"&&start.isChecked()) 
    {
        new PostTask3().execute("testing");
    }

The toast there shows: Start-up: 1 and Checked: true. Yet "if" fails to evaluate the expression as true and run PostTask3.

Help ... ?

user2015552
  • 115
  • 1
  • 2
  • 12
  • Why are you not using braces for `if().. else..` ? – TheLostMind Jul 28 '14 at 08:38
  • 2
    It's never a good idea to *assume* that your code is correct and that the platform you're using has bugs (as per "theoretically should work"). It's generally better to assume that you've got a bug, and you just haven't found it yet. – Jon Skeet Jul 28 '14 at 08:41

1 Answers1

0

To compare Strings use .equals().

So Change

 if(StartUp=="1"&&start.isChecked()) 

to

if(StartUp.equals("1") && start.isChecked())

For more info see How do I compare strings in Java?

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • it would be better to compare the constant with the object, and not the object with the constant – Blackbelt Jul 28 '14 at 08:41
  • 1
    @blackbelt: Not necessarily. If `StartUp` being null indicates a bug elsewhere, then a NullPointerException (thus alerting the developer) would probably be a better outcome than continuing in the face of incorrect assumptions. – Jon Skeet Jul 28 '14 at 08:42
  • @JonSkeet thanks for the input. For the developer is an useful hint, but still it is a bad user experience. From my point of view, at least – Blackbelt Jul 28 '14 at 08:47
  • @blackbelt: A crash is often a better user experience than doing the wrong thing (e.g. wiping their saved game or whatever). And the developer is more likely to notice is before it *gets* to the end user. – Jon Skeet Jul 28 '14 at 09:10