Same menu different Activities


Last Updated:

  1. Sam Voss

    Sam Voss Well-Known Member This Topic's Starter

    Joined:
    Jun 25, 2010
    Messages:
    367
    Likes Received:
    44
    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 (Text):
    1. 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.
    2.  
    3. 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
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458

    Create a class like so:
    Code (Text):
    1.  
    2. public class BaseActivity extends Activity
    3. {
    4.             @Override
    5.             public void onCreateOptionsMenu()
    6.             {
    7.                     //Construct your options menu here
    8.             }
    9.  
    10.             @Override
    11.             public void onOptionsItemSelected(...) // I forget teh parameters off the top of my head
    12.             {
    13.                     //handle your options menu item clicks here
    14.             }
    15. }
    16.  
    Then, anytime you have an activity that must include this option menu, just define it as so:

    Code (Text):
    1.  
    2. public class SubActivity extends BaseActivity
    3. {
    4.            @Override
    5.            public void onCreate(Bundle icicle)          
    6.            {
    7.                   super.onCreate(icicle);
    8.                   //blah blah blah
    9.            }
    10.          
    11.            // do w/e else you need to do...
    12. }
    13.  
     
    Sam Voss and andywhoa like this.
  3. Sam Voss

    Sam Voss Well-Known Member This Topic's Starter

    Joined:
    Jun 25, 2010
    Messages:
    367
    Likes Received:
    44
    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..
     
  4. Sam Voss

    Sam Voss Well-Known Member This Topic's Starter

    Joined:
    Jun 25, 2010
    Messages:
    367
    Likes Received:
    44
    Oh wait, so I'm extending something that extends activity pretty much?
     
  5. Sam Voss

    Sam Voss Well-Known Member This Topic's Starter

    Joined:
    Jun 25, 2010
    Messages:
    367
    Likes Received:
    44
    Alright cool, works like a charm. [close thread]
     

Share This Page

Loading...