title: Android - MenuItem layout order affect java code
body: <p>I tried to create a menu with search and refresh button, but since refresh button will only be available at several activities, I want to set the visibility to false, and then add this:</p>
  
  <pre><code>public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.mainmenu, menu);
      MenuItem item = menu.findItem(R.id.action_refresh);
      item.setVisible(true);
      return true;
  }
  </code></pre>
  
  <p>to the activity java</p>
  
  <p>Using the below code, the menu is displayed properly, with refresh on the left and search on the right</p>
  
  <pre><code>&lt;item
      android:id="@+id/action_search"
      android:title="Search"
      android:menuCategory="system"
      android:orderInCategory="2"
      android:showAsAction="always"
      android:icon="@drawable/ic_action_search"/&gt;
  
  &lt;item
      android:id="@+id/action_refresh"
      android:title="Refresh"
      android:menuCategory="system"
      android:orderInCategory="1"
      android:showAsAction="always"
      android:icon="@drawable/ic_action_refresh"
      android:visible="false"/&gt;
  </code></pre>
  
  <p>But by reversing the item order, </p>
  
  <pre><code>&lt;item
      android:id="@+id/action_refresh"
      android:title="Refresh"
      android:menuCategory="system"
      android:orderInCategory="1"
      android:showAsAction="always"
      android:icon="@drawable/ic_action_refresh"
      android:visible="false"/&gt;
  
  &lt;item
      android:id="@+id/action_search"
      android:title="Search"
      android:menuCategory="system"
      android:orderInCategory="2"
      android:showAsAction="always"
      android:icon="@drawable/ic_action_search"/&gt;
  </code></pre>
  
  <p>the refresh button/item will not appear.</p>
  
  <p>While I have found the workaround and could proceed, this still puzzles me very much, and I haven't got any good explanation.</p>
  
  <p>Why would that happen?</p>