tags:

views:

234

answers:

3

Hi,

I wanted 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.

Thanks

+7  A: 

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.

Dave Webb
Thanks a lot Dave :)
Funkyidol
A: 

great explanation, easy to reuse! Thx Dave, works perfectly now.

Sephy
A: 

Works beautifully. Thank you, solution 1 is puuurfect.

Lars Rye Jeppesen