Android Studio menu opens all items on every item pick

Issue

I am currently working on an Android Studio project, and I’ve stumbled into an issue, no matter which option I pick in the menu, it changes activity to all of them in a row, and then crashes.
Here’s my home page menu functions:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    menu.removeItem(R.id.HomePage);
    if (sp.getLong("User_Id", -1) != -1) {
        menu.removeItem(R.id.Login);
        menu.removeItem(R.id.SignUp);
    }
    else {
        menu.removeItem(R.id.LogOut);
    }
    if (sp.contains("User_Id")){
        if (!database.userDao().GetUserById(sp.getLong("User_Id", -1)).getUserEmail().equals(R.string.admin_email))
            menu.removeItem(R.id.UserList);
    }
    else
        menu.removeItem(R.id.UserList);
    return true;
}

@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.SignUp: {
            startActivity(new Intent(MainActivity.this, RegisterActivity.class));
        }
        case R.id.Login: {
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
        }
        case R.id.UserList: {
            startActivity(new Intent(MainActivity.this, UserActivity.class));
        }
        case R.id.LogOut:{
            builder.setMessage(R.string.logout_dialog_message).setTitle(R.string.logout_dialog_title).setCancelable(true).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    editor.clear();
                    editor.commit();
                    finish();
                    overridePendingTransition(0, 0);
                    startActivity(getIntent());
                    overridePendingTransition(0, 0);
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
        case R.id.About: {
            Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.custom_about_dialog);
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.setCancelable(true);
            dialog.getWindow().getAttributes().windowAnimations = R.style.animation;
            ImageView image = dialog.findViewById(R.id.IVLogo);
            TextView text = dialog.findViewById(R.id.TVText);
            image.setBackground(getDrawable(R.drawable.logo));
            text.setText("About us: We are regel2, the new best second hand app. In this app, you are able to buy and sell any product that you want, for any price that you want, all inside 1 app.");
            dialog.show();
        }
    }
    return super.onOptionsItemSelected(item);
}

Note: It opens even deleted menu items (Aka menu.removeItem(___)) and it doesn’t happen when I click on the about item, which may act differently because it’s a dialog.

Solution

You need to put

break;

as the last statement of every case block, otherwise execution will continue to the end.

Answered By – Master_T

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published