onOptionsItemSelected is not functioning properly

34 views Asked by At

I have two buttons on my action bar, a logout button, and a create new message button. However, if i click the create new message button (where nothing is supposed to happen yet), it logs me back out. My code is set as what it is supposed to do. Any suggestions?

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch(id) {
        case R.id.action_newPool:
            Toast.makeText(getApplicationContext(),"Being Implemented", Toast.LENGTH_LONG).show();

        case R.id.action_logOut:
            ParseUser.logOut();
            Intent leaveIntent = new Intent (MyPoolsActivity.this, DispatchActivity.class);
            leaveIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(leaveIntent);

    }
1

There are 1 answers

0
Brandon Ling On BEST ANSWER

You need a break keyword after your first case, otherwise it will execute the next case. This allows it to "break" out of the switch block.

switch(id) {
        case R.id.action_newPool:
            Toast.makeText(getApplicationContext(),"Being Implemented", Toast.LENGTH_LONG).show();
             break;
        case R.id.action_logOut:
            ParseUser.logOut();
            Intent leaveIntent = new Intent (MyPoolsActivity.this, DispatchActivity.class);
            leaveIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(leaveIntent);
            break;
    }