Emanuil Rusev
Emanuil Rusev

Reputation: 35235

Creating an options menu that is available to all Activities in an application

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?

Upvotes: 6

Views: 2566

Answers (3)

Mudit Jain
Mudit Jain

Reputation: 4213

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.

Upvotes: 0

Salsero69
Salsero69

Reputation: 2240

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.

Upvotes: 0

Pentium10
Pentium10

Reputation: 207830

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

Upvotes: 7

Related Questions