Static options menu
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?
If I read your question correctly you want the same menu in all your Activities. I can think of two ways to do this:
Create a subclass of
Activity
that implementsonCreateOptionsMenu()
andonOptionsItemSelected()
(and possiblyonPrepareOptionsMenu
). Then have all yourActivity
classes extend this subclass.Create a static method somewhere called something like
populateOptionsMenu()
that takes aMenu
(and probably aContext
) as arguments. YourActivity
classes can then call this from theironCreateOptionsMenu()
methods to populate theMenu
. You'd also need a correspondingprocessItemSelected()
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.
精彩评论