title: Android option menu inflating cause the SearchView focused
body: <p>I am buidingl a SearchableActivity which contains the search view. </p>
  
  <p>When user submit a search, the <code>SearchableActivity</code> will display the result, at the same time I will add one item in the Option menu name "To Result List" which will redirec to the ResultListActivity which display the search result in a List layout.</p>
  
  <p>The workflow can be descripbed as following:</p>
  
  <p>1 User click the Search icon in the action bar and input something and submit.</p>
  
  <p>2 The <code>SearchableActivity</code> receive the query in a new intent, and do the search job.</p>
  
  <p>3 Receive the result, display them. And the most important job, invalidate the Option menu by inflating the menu with a new menu layout file which contain the "To Result List" item.</p>
  
  <p>4 When user hit the  "To Result List", start the <code>ResultListActivity</code>.</p>
  
  <p>I have almost made the job:</p>
  
  <pre><code>public class SearchableActivity extends ActionBarActivity {
      private TextView textView;
      private SearchView searchView;
      private int status = 0;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.search_searchable);
          textView = (TextView) findViewById(R.id.result);
          handleIntent(getIntent());
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          switch (status) {
              case 0:
                  getMenuInflater().inflate(R.menu.home_default, menu);
                  break;
              case 1:
                  getMenuInflater().inflate(R.menu.home_2, menu);
                  break;
          }
          setupSearchView(menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      private void setupSearchView(Menu menu) {
          SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
          searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
          searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
          searchView.clearFocus();
      }
  
      @Override
      protected void onNewIntent(Intent intent) {
          setIntent(intent);
          handleIntent(intent);
      }
  
      private void handleIntent(Intent intent) {
          if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
              String query = intent.getStringExtra(SearchManager.QUERY);
              performSearch(query);
          }
      }
  
      private void performSearch(String query) {
          textView.setText(query + " result \n");
          status = 1;
          searchView.clearFocus();
          ActivityCompat.invalidateOptionsMenu(this);
      }
  }
  </code></pre>
  
  <p>home_default.xml:</p>
  
  <pre><code>...
      &lt;item
          android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/ic_menu_search"
          com.demo:actionViewClass="android.support.v7.widget.SearchView"
          com.demo:showAsAction="always|collapseActionView"/&gt;
  ...
  </code></pre>
  
  <p>home_2.xml:</p>
  
  <pre><code>...
      &lt;item
          android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/ic_menu_search"
          com.demo:actionViewClass="android.support.v7.widget.SearchView"
          com.demo:showAsAction="always|collapseActionView"/&gt;
      &lt;item
          android:id="@+id/action_toResultList"
          android:title="@string/action_toList"
          com.demo:showAsAction="always"/&gt;
  ...
  </code></pre>
  
  <p>However, I meet some problems:</p>
  
  <p>1 At the workflow step 3, I will invaldate the option menu to add the new item, however I found that if the menu is inflated again, the <code>SerchView</code> will be focused and the Input keyborad will show up, like this:</p>
  
  <p><img src="https://i.stack.imgur.com/IRg74.png" alt="enter image description here">  </p>
  
  <p>As the code shown, I have call the <code>searchView.clearFocus()</code> but it seem that it does not work.</p>
  
  <p>And if I comment the line </p>
  
  <pre><code>ActivityCompat.invalidateOptionsMenu(this);
  </code></pre>
  
  <p>After search, the screen will be this:</p>
  
  <p><img src="https://i.stack.imgur.com/kwAy2.png" alt="enter image description here"></p>
  
  <p>2 When I change the screen orientation after search , I will got a <code>NullPointException</code>  at line <code>searchView.clearFocus();</code> and the app will exit.</p>
  
  <p>What's the problem?</p>