Fix search back behavior.

This is the final issue: we remove the intermediate activity when
searching.  After this, back at search results goes directly to the Inbox.

Change-Id: I437d7bd7b08da1d7befdd873b43edaeee6ac99ee
diff --git a/src/com/android/mail/ui/ActionBarView.java b/src/com/android/mail/ui/ActionBarView.java
index f021db7..31bdbbc 100644
--- a/src/com/android/mail/ui/ActionBarView.java
+++ b/src/com/android/mail/ui/ActionBarView.java
@@ -53,7 +53,8 @@
  * TODO(viki): Include ConversationSubjectDisplayer here as well.
  */
 public final class ActionBarView extends LinearLayout implements OnNavigationListener,
-        ViewMode.ModeChangeListener, OnQueryTextListener, OnSuggestionListener {
+        ViewMode.ModeChangeListener, OnQueryTextListener, OnSuggestionListener,
+        MenuItem.OnActionExpandListener {
     private ActionBar mActionBar;
     private RestrictedActivity mActivity;
     private ActivityController mController;
@@ -107,7 +108,7 @@
     }
 
     /**
-     * Collapses the search action view.
+     * Close the search view if it is expanded.
      */
     public void collapseSearch() {
         if (mSearch != null) {
@@ -123,6 +124,7 @@
         mSearch = menu.findItem(R.id.search);
         if (mSearch != null) {
             mSearchWidget = (SearchView) mSearch.getActionView();
+            mSearch.setOnActionExpandListener(this);
             SearchManager searchManager = (SearchManager) mActivity.getActivityContext()
                     .getSystemService(Context.SEARCH_SERVICE);
             if (searchManager != null && mSearchWidget != null) {
@@ -130,6 +132,7 @@
                 mSearchWidget.setSearchableInfo(info);
                 mSearchWidget.setOnQueryTextListener(this);
                 mSearchWidget.setOnSuggestionListener(this);
+                mSearchWidget.setIconifiedByDefault(true);
             }
         }
         mHelpItem = menu.findItem(R.id.help_info_menu_item);
@@ -459,4 +462,24 @@
     public void onFolderUpdated(Folder folder) {
         mSpinner.onFolderUpdated(folder);
     }
+
+    @Override
+    public boolean onMenuItemActionExpand(MenuItem item) {
+        // Do nothing. Required as part of the interface, we ar only interested in
+        // onMenuItemActionCollapse(MenuItem).
+        // Have to return true here. Unlike other callbacks, the return value here is whether
+        // we want to suppress the action (rather than consume the action). We don't want to
+        // suppress the action.
+        return true;
+    }
+
+    @Override
+    public boolean onMenuItemActionCollapse(MenuItem item) {
+        // When the action menu is collapsed, we have performed a search, pop the search fragment.
+        mController.exitSearchMode();
+        // Have to return true here. Unlike other callbacks, the return value here is whether
+        // we want to suppress the action (rather than consume the action). We don't want to
+        // suppress the action.
+        return true;
+    }
 }