i have this problem which i never expect it to occur. When i pressed on the Menu button, the program display the Menu and after i click on one of the items from the Menu, it pops out a item list for me to choose. However, just when the item list pops out, some of the code in onOptionsItemSelected() was called when i did not even select any item. After i selected on of the items, onOptionsItemSelected() got called again (i know this is called because an item was selected). Can anyone tell me how to solve this problem or even why is this happening? Below is the coding…
Btw, the ‘count’ is just an integer variable to show whether onOptionsItemSelected() was called as it would increment each time when called.
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sendMenu = menu.addSubMenu("Change Profile");
int size = profileNames.size();
for(int i=0; i<size; i++){
sendMenu.add(0,i,0,profileNames.get(i).toString());
}
menu.add(1, size, 0, "Configuration");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
count++;
Toast toast1 = Toast.makeText(this, "Count: "+count, Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast1.show();
selectedProfileName = null;
//change the map according to the selected profile name
if(item.getItemId()<=profileNames.size()-1){
selectedProfileName = profileNames.get(item.getItemId());
setContentView(new SampleView(this));
}
else{
Intent myIntent=new Intent(this,configurationTabWidget.class);
startActivity(myIntent);
}
//just to re-draw the map with the new selected profile name
return true;
}
You are selecting two items – first you select the “Change Profile” menu item, and then you select the particular item from the SubMenu.