确认对话框为操作栏上的所有项目打开 onClick

Confirm Dialog Opens onClick for All Items on Action Bar

您好,我是 android 的新手,我的操作栏上有几个项目,还有一个注销 icon/button。现在,当用户单击注销按钮时,它应该显示一个确认对话框,如果用户单击 "Yes".

,则返回登录屏幕

我已设法做到这一点,但我注意到现在显示的是我单击的所有项目的确认对话框,而不仅仅是注销。

这是我的代码:

@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_open_invoices:
                Intent i = new Intent(context, InvoicesActivity.class);
                startActivity(i);

            case R.id.action_synchronize:
                // Start Updater Service
                Intent updaterService = new Intent(context, UpdaterService.class);
                startService(updaterService);

            case R.id.action_logout:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);

                builder.setTitle("Confirm");
                builder.setMessage("Are you sure?");

                builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        settings.edit().remove("Token").commit();

                        Intent loginIntent = new Intent(context, LoginActivity.class);
                        startActivity(loginIntent);

                        dialog.dismiss();
                    }

                });

                builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do nothing
                        dialog.dismiss();
                    }
                });

                AlertDialog alert = builder.create();
                alert.show();

        }

        return super.onOptionsItemSelected(item);
    }
case R.id.action_open_invoices:
            Intent i = new Intent(context, InvoicesActivity.class);
            startActivity(i);
       break;

使用 break 语句