简体   繁体   中英

Menu Items not working when i hit menu items

i am implementing side navigation bar, i`ll create onOptionsItemSelected () method inside method codes are

`

public boolean onOptionsItemSelected(@NonNull MenuItem item) {
     if (toggle.onOptionsItemSelected(item))return true;

    switch (item.getItemId()){
        case R.id.logoutId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();
        case R.id.homeId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();
        case R.id.profileId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();
        case R.id.settingsId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();

    }

    return super.onOptionsItemSelected(item);
}

`

every thing is fine but clicking action not working.? plz help me!!!

update:

操作栏菜单项 侧边导航项 not working!

Action bar item worked well but

but Side Navigation Item not working? plz tell me the solution and why its not working ?

You should return true when you handle menu item click. Try to add the return statement on each case.

For example:

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.logoutId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();
            return true;
        case R.id.homeId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();
            return true;
        case R.id.profileId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();
            return true;
        case R.id.settingsId:
            Toast.makeText(getApplicationContext() , item.getItemId() , Toast.LENGTH_SHORT).show();
            return true;
        case default:
            return super.onOptionsItemSelected(item);
    }

    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM