Put a break
statement at the end of each case of the switch!
Android Up Navigation and Button in Action Bar
-
03-06-2023 - |
题
I have an action bar that looks like so:
I am using the following to see which button is clicked:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.view_all_trains:
Intent i = new Intent(ToStationActivity.this, MapActivity.class);
startActivityForResult(i, 0);
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
No matter what button you press, (the back or the pin), it goes back to the previous screen and I am not sure why. Any suggestions as to why this may happen?
解决方案
其他提示
You need to finish the case at the end using break;
, else the control flow continues at the next case.
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.view_all_trains:
Intent i = new Intent(ToStationActivity.this, MapActivity.class);
startActivityForResult(i, 0);
break;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
不隶属于 StackOverflow