Milos Milosanovic
Milos Milosanovic

Reputation: 27

Can't make button invisible from another activity

First, I click on Go to level one button in MainActivity to go to the LevelOne Activity from MainActivity, then I am going back to MainActivity and now here I need Go to level one button to be INVISIBLE. I tried to accomplish this via SharedPreferences, but couldn't make it. The code I posted below is working just fine, it just doesn't make my Go to level one button invisible when I go back to MainActivity.

I've done this so far:

MainActivity.java:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bLevelOne:
            Intent i = new Intent(this, LevelOne.class);
            startActivityForResult(i, 1);
            break;
        case R.id.bLevelTwo:

            break;
        case R.id.bLevelThree:

            break;
        case R.id.bLevelFour:

            break;
        case R.id.bLevelFive:

            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String result = data.getStringExtra("result");
                if (result == "milos") {
                    level1.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

LevelOne.java:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.bMenu:
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", "milos");
            setResult(RESULT_OK, returnIntent);
            finish();
            break;
        case R.id.bNextLevel:

            break;
        }
    }

Upvotes: 0

Views: 78

Answers (1)

Anuj
Anuj

Reputation: 2073

you have a problem over here.

 if (result == "milos") {
                level1.setVisibility(View.INVISIBLE);
            }

it should rather be

 if (result.equalsIgnoreCase("milos") {
                level1.setVisibility(View.INVISIBLE);
            }

Upvotes: 2

Related Questions