title: Getting Switch instance inside ActionBar
body: <p>I managed to put a Switch inside the action bar (as in the Wi-Fi settings). </p>
  
  <p>I put the following <strong>mainmenu.xml</strong> file inside the /menu folder:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  &lt;item android:id="@+id/item1" 
        android:titleCondensed="Service" 
        android:title="Service"
        android:actionViewClass="android.widget.Switch"
        android:showAsAction="always|withText"&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p></p>
  
  <p>After that I overrode the <code>onCreateOptionsMenu()</code> method in the activity, as follows:</p>
  
  <pre><code>@Override
  public boolean onCreateOptionsMenu(Menu menu) 
  {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.mainmenu, menu);
  
      // Get widget's instance
      swtService = (Switch)menu.findItem(R.id.item1).getActionView();
      swtService.setOnCheckedChangeListener(this); 
  
      return super.onCreateOptionsMenu(menu);
  }
  </code></pre>
  
  <p>Unfortunately, I can't understand when this method is called.
  Here's the problem: it seems that <code>onCreateOptionsMenu</code> is not called even before <code>onResume()</code>, so a <code>NullPointerException</code> is thrown:</p>
  
  <pre><code>@Override
  public void onResume()
  {
      super.onResume();
  
      // Synchronize the switch with service's status
      swtService.setChecked(ServiceHelper.isServiceStarted(this, MySystemService.class.getName()));
  }
  </code></pre>
  
  <p>Am I missing something?
  Is there another way to put a View inside the action bar?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>My target API is 17, and I don't care about lower ones. :)</p>
  
  <p>Here's a shot of the application, showing the lifecycle methods called:
  <img src="https://i.stack.imgur.com/cIOj0.png" alt="Lifecycle methods"></p>
  
  <p>Thanks</p>