1

I am new to Android Studio. In my project, I am trying to use IF statement in a very simple setting (to activate a change) but it doesn't work. Your help will be greatly appreciated. Below is my code:

public class MainActivity extends AppCompatActivity { private Button mBtLaunchActivity; private Button Home; int a = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Home = (Button) findViewById(R.id.home_button); mBtLaunchActivity = (Button) findViewById(R.id.map_button); mBtLaunchActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { launchActivity(); a = 1; } }); } private void launchActivity() { if (a == 1) { Home.setVisibility(View.INVISBLE); } Intent intent = new Intent(this, Activity3.class); startActivity(intent); } } 

1 Answer 1

3

You are updating value of a after launch of Activity.That will never work.

Update it before it:

@Override public void onClick(View view) { launchActivity(); a = 1; } 

Change it:

@Override public void onClick(View view) { a = 1; launchActivity(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.