Question

I have an action bar that looks like so:

Action Bar

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?

Was it helpful?

Solution

Put a break statement at the end of each case of the switch!

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top