Take the 2-minute tour ×

I want to create a static options menu for all my activity screens. I dont want to override onCreateOptionsMenu() in each activity.

Since Menu class is an interface with a huge number of methods, its difficult to create a static object of the implementing class.

Any other way of doing the same?

share|improve this question
add comment

1 Answer

up vote 16 down vote accepted

If I read your question correctly you want the same menu in all your Activities. I can think of two ways to do this:

  1. Create a subclass of Activity that implements onCreateOptionsMenu() and onOptionsItemSelected() (and possibly onPrepareOptionsMenu). Then have all your Activity classes extend this subclass.

  2. Create a static method somewhere called something like populateOptionsMenu() that takes a Menu (and probably a Context) as arguments. Your Activity classes can then call this from their onCreateOptionsMenu() methods to populate the Menu. You'd also need a corresponding processItemSelected() static method for when items were clicked.

Option 1 seems best as it wouldn't require the same bolierplate in your Activities to call the static methods.

share|improve this answer
Thanks a lot Dave :) –  Funkyidol Jan 18 '10 at 4:33
Do you mean Option 2 wouldn't require the same bolierplate? –  jondavidjohn Jun 27 '11 at 17:55
The first solution is not good idea because it requires inheritance. What if my Activities are other than Activity (FragmentActivity, ListActivity, ...)? I think we should void inheritance when possible. –  Emerald214 Jul 9 '12 at 7:33
add comment

Your Answer


