문제

My problem is, that I have got a TabActivity, which has 4 Tabs right now. The first Tab is a special Details-Tab, where the user could modify some data.

The problem is, that if I add a OptionsMenu for the Activity, that the OptionsMenu is appearing on every Tab. I tried to check the current mTabHost.getCurrentTabTag() in the onCreateOptionsMenu but that changed nothing. So, how to do that?

(The following code, which still shows the OptionsMenu on every Tab)

 public boolean onCreateOptionsMenu(Menu menu) {    
  if(mTabHost.getCurrentTabTag()==getString(R.string.tab_details)) {
   boolean result = super.onCreateOptionsMenu(menu);
   menu.add(0, EDIT_ID, 0, R.string.menu_edit).setIcon(R.drawable.edit);
   return result;
  }    

  return true;
 }

 public boolean onMenuItemSelected(int featureId, MenuItem item) {
  switch (item.getItemId()) {
       case EDIT_ID: {   
        Toast.makeText(this, "o.O", Toast.LENGTH_LONG).show();   
       }
  }

  return super.onMenuItemSelected(featureId, item);
 } 
도움이 되었습니까?

해결책

I see that you are comparing strings (tags in fact) with ==, usually, this is nicer to do that with .equals() method. Maybe it will solve your issue.
To dynamically update an option menu (from the documentation) :

If you want to change the Options Menu each time it opens, you must override the onPrepareOptionsMenu()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top