onOptionsItemSelected运行不正常

时间:2015-06-10 21:08:01

标签: java android android-actionbar case

我的操作栏上有两个按钮,一个注销按钮和一个创建新消息按钮。但是,如果我单击“创建新消息”按钮(尚未发生任何事情),它会将我退回。我的代码设置为它应该做的。有什么建议吗?

    @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 个答案:

答案 0 :(得分:1)

您的第一个案例后需要break个关键字,否则会执行下一个案例。这使它能够“打破”#34;离开开关区。

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;
    }