Strange menu problem with two activities

Put your problem here if it does not fit any of the other categories.

Strange menu problem with two activities

Postby SlumberMachine » Wed Oct 21, 2009 11:04 am

my app has two activities, but for some reason my menu item from the first activity is being added to the menu in the second activity.

first activity is just a view with four buttons and an "exit" button in the menu. Once one of the four main buttons are pressed I start the second activity which displays data in a scrollview and has a bunch of menu options (save, clear, goback, email). I made 2 different menu (mainmenu.xml and menu.xml) xml files. One for each activity and I call them each using similar code in each activity using the menu inflator method. For some reason "exit" is showing up at the top of my second activities menu. I triiple checked and I did not accidentally add it to the second acivities menu xml or code.

I also tried menu.clear() before creating the second menu and that just made it have no menu. Anyone know how to properly handle menus in a multi activity app?

I looked here, but still did not find an answer: http://developer.android.com/guide/prac ... esign.html

Thanks for any help!
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Top

Postby SlumberMachine » Thu Oct 22, 2009 10:27 am

So I somewhat solved this issue and just wanted to post here the answer in case someone else runs into the same thing. :D

There must be someway to handle this issue using the menu inflator method for making menus, but I have been unable to find anything at all on it. So I switched to the menu.add method and removed the menu.xml files.

So now I use the following code for handling the menu in the first(main) activity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static final int INSERT_ID = Menu.FIRST;
  2.  
  3. @Override
  4.  
  5. public boolean onCreateOptionsMenu(Menu menu) {
  6.  
  7.     boolean result = super.onCreateOptionsMenu(menu);
  8.  
  9.     //menu.add is (Group # , Int ID , order in the menu , text of the item)
  10.  
  11.     menu.add(0, INSERT_ID, 0, R.string.menu_quit);
  12.  
  13.     return result;
  14.  
  15. }
  16.  
  17.  
  18.  
  19.  
  20.  
  21. /* Handles menu item selections */
  22.  
  23. public boolean onOptionsItemSelected(MenuItem item) {
  24.  
  25.     switch (item.getItemId()) {
  26.  
  27.     case INSERT_ID:
  28.  
  29.         Toast.makeText(this, "Bye!", Toast.LENGTH_SHORT).show();
  30.  
  31.         finish();
  32.  
  33.         return true;
  34.  
  35.     }
  36.  
  37.     return false;
  38.  
  39. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Second activity, you can turn off which group is visible using the line "menu.setGroupVisible(0, false);":

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static final int MENUITEM_ID = Menu.FIRST;
  2.  
  3.  
  4.  
  5.         @Override
  6.  
  7.         public boolean onCreateOptionsMenu(Menu menu) {
  8.  
  9.             boolean result = super.onCreateOptionsMenu(menu);
  10.  
  11.             menu.setGroupVisible(0, false);
  12.  
  13.             menu.add(1, MENUITEM_ID, 0, "test item");
  14.  
  15.             //menu.findItem(R.id.quit).setEnabled(true);
  16.  
  17.             return result;
  18.  
  19.         }
  20.  
  21.  
  22.  
  23.         /* Handles item selections */
  24.  
  25.         public boolean onOptionsItemSelected(MenuItem item) {
  26.  
  27.             switch (item.getItemId()) {
  28.  
  29.             case MENUITEM_ID:
  30.  
  31.                 Toast.makeText(this, "test item pressed", Toast.LENGTH_SHORT).show();
  32.  
  33.                 return true;
  34.  
  35.             }
  36.  
  37.             return false;
  38.  
  39.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 23 guests