I’ve been coding for a bit trying to link two activties and I’m nearly there but I’ve run into a problem.
A line of my coding is unreachable. Can anyone explain why?
package com.example.twolink
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MainActivity2.class));
finish();
}
});
}
Initialize your Button on the onCreate method like so :
If you want to hide the button initially, use the
setVisibility(View.GONE)to toggle the button visibility. Then, if you want to show it later, you cansetVisibility(View.VISIBLE)elsewhere.