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 开发者_StackOverflow社区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?
Subclass Activity
to have your own activity class, and extend
that to inherit some functionality like OptionsMenu.
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.
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.
精彩评论