Home > Mobile >  Android Studio menu opens all items on every item pick
Android Studio menu opens all items on every item pick

Time:11-26

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.

CodePudding user response:

You need to put

break;

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

  • Related