title: OptionsMenu items common for multiple activites
body: <p>The Android <a href="http://developer.android.com/design/patterns/help.html" rel="nofollow">design guide</a> says that Help should always be placed as the last item of the overflow menu (it should never appear in the ActionBar) and also, that is should be present in every activity, so that users don't have to look for it. Similar approach is also recommended for Settings.</p>
  
  <p>However, I'm wondering what is the best way to make sure that all the activities in my app handle these items without lots of code repetition? Putting these common items manually into every XML menu file and then manually handling clicks on each of them in every activity class is just nonsense.</p>
  
  <p>Since I am already extending all of my activities from a common base class (which provides some convenience methods), I came up with this approach: In the <code>BaseActivity</code> class, I define an empty <code>initOptionsMenu()</code> method which the subclasses may override (<em>template method pattern</em> style) by adding their specific items to the menu. This method is called at the start of <code>onCreateOptionsMenu()</code> and then the base class adds the common items (settings and help) at the end of the menu.</p>
  
  <p>The <code>onOptionsItemSelected()</code> method follows the standard pattern - it switches on the item ID and in the default case, it passes the handing to the superclass. Again, the base class handles the setting and help cases.</p>
  
  <pre><code>public class BaseActivity extends Activity {
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          initOptionsMenu(menu);
          menu.add(Menu.NONE, R.id.menu_settings, Menu.NONE, R.string.menu_help);
          menu.add(Menu.NONE, R.id.menu_help, Menu.NONE, R.string.menu_help);
          return true;
      }
  
      protected void initOptionsMenu(Menu menu) {}
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.menu_settings:
                  startActivity(new Intent(this, SettingsActivity.class));
                  return true;
              case R.id.menu_help:
                  startActivity(new Intent(this, HelpActivity.class));
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  }
  </code></pre>
  
  <p>Now, whenever I create a new activity, I extend the <code>BaseActivity</code>. If the derived activity provides some more items for the options menu, I do not override the standard <code>onOptionsItemSelected()</code>, but instead I override the custom initOptionsMenu() method and populate the menu there. In <code>onOptionsItemSelected()</code>, I need to handle only cases for the items specific for this activity, the common ones will be handled by the <code>super</code> call.</p>
  
  <pre><code>public class FooActivity extends BaseActivity {
  
      @Override
      protected void initOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.foo, menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              // cases for items in R.menu.foo
  
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  }
  </code></pre>
  
  <p><strong>Is this pattern sensible? Can you come up with a better approach? Please share your thoughts.</strong></p>