Make sure to update menu options when switching tabs

Also removed unnecessarily complicated code to invalidate menu options
when entering/exiting the search mode.

Bug 5027867

Change-Id: I0ee24975d327b775c64952abfbdcbcf32e618a0d
diff --git a/src/com/android/contacts/activities/PeopleActivity.java b/src/com/android/contacts/activities/PeopleActivity.java
index 907a5fd..500ad1e 100644
--- a/src/com/android/contacts/activities/PeopleActivity.java
+++ b/src/com/android/contacts/activities/PeopleActivity.java
@@ -145,10 +145,6 @@
     private int mProviderStatus = -1;
 
     private boolean mOptionsMenuContactsAvailable;
-    /** true if the search menu item exists */
-    private boolean mOptionsMenuSearchExists;
-    /** true if the search menu item is currently visible */
-    private boolean mOptionsMenuSearchVisible;
 
     /**
      * Showing a list of Contacts. Also used for showing search results in search mode.
@@ -483,12 +479,12 @@
             case START_SEARCH_MODE:
                 clearSearch();
                 updateFragmentsVisibility();
-                invalidateOptionsMenuIfNeeded();
+                invalidateOptionsMenu();
                 break;
             case STOP_SEARCH_MODE:
                 clearSearch();
                 updateFragmentsVisibility();
-                invalidateOptionsMenuIfNeeded();
+                invalidateOptionsMenu();
                 break;
             case CHANGE_SEARCH_QUERY:
                 loadSearch(mActionBarAdapter.getQueryString());
@@ -521,6 +517,7 @@
                     mTabPager.setCurrentItem(tab.ordinal(), false /* no smooth scroll */);
                 }
             }
+            invalidateOptionsMenu();
             return;
         }
 
@@ -576,6 +573,7 @@
             ft.commit();
             fragmentManager.executePendingTransactions();
         }
+        invalidateOptionsMenu();
     }
 
     private class TabPagerListener implements ViewPager.OnPageChangeListener {
@@ -592,6 +590,7 @@
             // Make sure not in the search mode, in which case position != TabState.ordinal().
             if (!mTabPagerAdapter.isSearchMode()) {
                 mActionBarAdapter.setCurrentTab(TabState.fromInt(position), false);
+                invalidateOptionsMenu();
             }
         }
     }
@@ -1074,9 +1073,6 @@
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.actions, menu);
 
-        mOptionsMenuSearchExists = menu.findItem(R.id.menu_search) != null;
-        mOptionsMenuSearchVisible = mOptionsMenuSearchExists;
-
         // On narrow screens we specify a NEW group button in the {@link ActionBar}, so that
         // it can be in the overflow menu. On wide screens, we use a custom view because we need
         // its location for anchoring the account-selector popup.
@@ -1107,10 +1103,6 @@
             return true;
         }
 
-        if (mOptionsMenuSearchVisible != shouldMakeSearchMenuVisible()) {
-            return true;
-        }
-
         if (mAllFragment != null && mAllFragment.isOptionsMenuChanged()) {
             return true;
         }
@@ -1127,11 +1119,6 @@
         return false;
     }
 
-    /** @return true if the search menu item should be visible */
-    private boolean shouldMakeSearchMenuVisible() {
-        return mOptionsMenuSearchExists && !mActionBarAdapter.isSearchMode();
-    }
-
     @Override
     public boolean onPrepareOptionsMenu(Menu menu) {
         mOptionsMenuContactsAvailable = areContactsAvailable();
@@ -1171,8 +1158,7 @@
 
         if (searchMenu != null) {
             // Don't show the search menu in search mode.
-            mOptionsMenuSearchVisible = shouldMakeSearchMenuVisible();
-            searchMenu.setVisible(mOptionsMenuSearchVisible);
+            searchMenu.setVisible(!mActionBarAdapter.isSearchMode());
         }
 
         MenuItem settings = menu.findItem(R.id.menu_settings);