Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 583
Kshitij Aggarwal Avatar asked Dec 04 '22 13:12

Kshitij Aggarwal


1 Answers

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.

like image 131
Dave Webb Avatar answered Jan 12 '23 03:01

Dave Webb