6

I want to have an options menu that is available to all Activities in my app (on pressing the MENU button). I've been doing this by creating it in onCreateOptionsMenu(Menu menu) for each Activity but this seems redundant.

Is there a way to create it in one place and have it available in all Activities?

Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201

3 Answers3

7

Subclass Activity to have your own activity class, and extend that to inherit some functionality like OptionsMenu.

Pentium10
  • 204,586
  • 122
  • 423
  • 502
0

If your other class extends Activity then change it to extend the new OptionsMenuActivity class created by you. However if it extends one of the subclasses of Activity, such as ListActivity, MapActivity etc then I guess either you can either extend each of these subclasses with options menu functionality or simply repeat the options menu code for each Activity.

Dmitrii Leonov
  • 1,331
  • 1
  • 15
  • 25
Mudit Jain
  • 4,163
  • 1
  • 22
  • 18
0

I've been trying to figure this out as well. Like what everyone else says, I've been extending Activity in a class called BaseActivity.

The annoyance with this is when your activity extends ListActivity or some other activity class then you also need to extend all those other activity classes as well.

There is a Java technology that I don't quite remember the name but is something similar to C's #define statement which allows for code injection.

Salsero69
  • 2,160
  • 3
  • 23
  • 27