user2975969
user2975969

Reputation: 1

How to differentiate MenuItems

I am creating a menu, but both of menu options were intent to same class so how to fix this? Sorry I am a starter developer, so, please give me solution of that problem.

public void btnclick(View v){

    openOptionsMenu();
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    menu.add(0, 1, 0, "What's in it");
    menu.add(0, 2, 0,"send");

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    Intent intt=new Intent(this,Help.class);
    startActivity(intt);
    return true;
}

Upvotes: 0

Views: 47

Answers (1)

nikis
nikis

Reputation: 11254

Since you are adding 2 menu items at the onCreateOptionsMenu: 1st with ItemId = 1 and 2nd with ItemId = 2, you can use getItemId() to distinguish between menu items, like so:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){
        case 1:
            //do stuff like:
            Intent intt=new Intent(this,Help.class);
            startActivity(intt);
            break;
        case 2:
            //do another stuff, like launching another activity
            break;
        default:
            break;
    }
    return true;

}

Upvotes: 1

Related Questions