user3481441
user3481441

Reputation:

App going to wrong Activity. While back-button goes to right one

my problem is as following:

When I click a button in my ActionBar it goes to the wrong activity. While when I am on the wrong activity and press the back-button. It goes to the one it was supposed to go to.

It's supposed to go to the AddActivity when the button is pressed.

If any other code is requested, please ask.

MapsActivity

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private Location mLastLocation;
    private Marker mCurrLocationMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkLocationPermission();
        }

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        initialize();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_marker:
                // User chose the "Add marker" item
                Intent addActivityIntent = new Intent(MapsActivity.this, AddActivity.class);
                startActivity(addActivityIntent);
            case R.id.action_logout:
                // User chose the "Uitloggen" item
                PokeMapperModel.getInstance().setCurrentUser(null);
                Intent loginActivityIntent = new Intent(MapsActivity.this, LoginActivity.class);
                startActivity(loginActivityIntent);
                finish();
            default:
                // Action not recognized
                return super.onOptionsItemSelected(item);
        }
    }

    private boolean isUserLoggedIn() {
        return PokeMapperModel.getInstance().getCurrentUser() != null;
    }

    private void initialize() {
        if (!isUserLoggedIn()) {
            Intent intent = new Intent(MapsActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

Upvotes: 0

Views: 424

Answers (2)

Nishant Pardamwar
Nishant Pardamwar

Reputation: 1485

you forgot to add break; after every case of switch statement.

EDIT: YOU DONT NEED TO ADD default:, check the updated code.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_marker:
                // User chose the "Add marker" item
                Intent addActivityIntent = new Intent(MapsActivity.this, AddActivity.class);
                startActivity(addActivityIntent);
                break;
            case R.id.action_logout:
                // User chose the "Uitloggen" item
                PokeMapperModel.getInstance().setCurrentUser(null);
                Intent loginActivityIntent = new Intent(MapsActivity.this, LoginActivity.class);
                startActivity(loginActivityIntent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

Upvotes: 4

Omar Aflak
Omar Aflak

Reputation: 2962

You need to return true;

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.one:
            one();
            return true;
        case R.id.two:
            two();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Upvotes: 0

Related Questions