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 implements onCreateOptionsMenu()
and onOptionsItemSelected()
(and possibly onPrepareOptionsMenu
). Then have all your Activity
classes extend this subclass.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With