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

Apps Same menu different Activities

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...
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones