• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Same menu different Activities

Sam Voss

Android Enthusiast
Hey everyone, I'd like to know, is it possible to have the same menu run throughout an entire application? I'd like to just have one menu, as its all I need.

Okay, so I'm sure someones going to bring this up, (see below) and if you do, before you just send me a link, explain to me how to do this, because you cannot extend more than once class in java, so please, how do you extend to Activity and to my other class?

Code:
Tip: If your application contains multiple activities and some of them provide the same Options Menu, consider creating an Activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemSelected() methods. [U][B][I]Then extend this class for each Activity that should share the same Options Menu.[/I][/B][/U] This way, you have to manage only one set of code for handling menu actions and each decendent class inherits the menu behaviors.

If you want to add menu items to one of your decendent activities, override onCreateOptionsMenu() in that Activity. Call super.onCreateOptionsMenu(menu) so the original menu items are created, then add new menu items with menu.add(). You can also override the super class's behavior for individual menu items
 
Hey everyone, I'd like to know, is it possible to have the same menu run throughout an entire application? I'd like to just have one menu, as its all I need.

Okay, so I'm sure someones going to bring this up, (see below) and if you do, before you just send me a link, explain to me how to do this, because you cannot extend more than once class in java, so please, how do you extend to Activity and to my other class?

Code:
Tip: If your application contains multiple activities and some of them provide the same Options Menu, consider creating an Activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemSelected() methods. [U][B][I]Then extend this class for each Activity that should share the same Options Menu.[/I][/B][/U] This way, you have to manage only one set of code for handling menu actions and each decendent class inherits the menu behaviors.
 
If you want to add menu items to one of your decendent activities, override onCreateOptionsMenu() in that Activity. Call super.onCreateOptionsMenu(menu) so the original menu items are created, then add new menu items with menu.add(). You can also override the super class's behavior for individual menu items


Create a class like so:
Code:
public class BaseActivity extends Activity
{
            @Override
            public void onCreateOptionsMenu()
            {
                    //Construct your options menu here
            }
 
            @Override
            public void onOptionsItemSelected(...) // I forget teh parameters off the top of my head
            {
                    //handle your options menu item clicks here
            }
}

Then, anytime you have an activity that must include this option menu, just define it as so:

Code:
public class SubActivity extends BaseActivity
{
           @Override
           public void onCreate(Bundle icicle)          
           {
                  super.onCreate(icicle);
                  //blah blah blah
           }
          
           // do w/e else you need to do...
}
 
Alright so that's exactlly what I have in my program, although its just there, not calling it, vecause how do I get to the sub activity? This has to be a stupid question that I'm missing something obvious here..
 
Back
Top Bottom