How to reload Android menu in the same activity?

I have a menu in an Android app. When I click on add favorites, I need to reload the menu options so that they appear in the favorite options and do not display the add favorites.

I do not want to use reboot due to the back button.

My code is:

public boolean onCreateOptionsMenu(Menu menu) { try { MenuItem menuInicio = menu.add(INICIO, INICIO, 0, "InΓ­cio"); menuInicio.setIcon(android.R.drawable.ic_menu_edit); MenuItem menuBusca = menu.add(BUSCA, BUSCA, 0, "Buscar"); menuBusca.setIcon(android.R.drawable.ic_menu_search); SubMenu menuFavoritos = menu.addSubMenu(FAVORITOS, FAVORITOS, 0, "Favoritos"); if(!phytoterapicContent.getPhytoterapicItem().getIsFav()) menuFavoritos.add(FAVORITOS, ADD_FAV, 0, "Adicionar aos Favoritos"); else menuFavoritos.add(FAVORITOS, DEL_FAV, 1, "Remover dos Favoritos"); menuFavoritos.add(FAVORITOS, LIST_FAV, 2, "Listar Favoritos"); menuFavoritos.setIcon(android.R.drawable.star_off); } catch (Exception e) { } return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case INICIO: Intent it = new Intent(ShowPhytoterapicActivity.this, HomeActivity.class); startActivity(it); break; case BUSCA: Intent it3 = new Intent(ShowPhytoterapicActivity.this, ShowSearchParametersActivity.class); startActivity(it3); break; case ADD_FAV: try { Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao(); phytoterapicContent.getPhytoterapicItem().setIsFav(true); phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem()); Toast.makeText(ShowPhytoterapicActivity.this, "Adicionado aos Favoritos", Toast.LENGTH_LONG).show(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case DEL_FAV: try { Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao(); phytoterapicContent.getPhytoterapicItem().setIsFav(false); phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem()); Toast.makeText(ShowPhytoterapicActivity.this, "Removido dos Favoritos", Toast.LENGTH_LONG).show(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case LIST_FAV: Intent it5 = new Intent(ShowPhytoterapicActivity.this, ShowFavoritesActivity.class); startActivity(it5); break; } return true; } 

Thanks!

+8
android android-menu
source share
2 answers

use onPrepareOptionsMenu

Prepare a menu of standard screen settings to be displayed. This is called right before the menu is displayed every time it is displayed. You can use this method to effectively turn on / off elements or otherwise dynamically change content.

 @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.clear(); if(isChangedStat) { menu.add(0, MENUITEM, 0, "True"); } else { menu.add(0, MENUITEM, 0, "False"); } return super.onPrepareOptionsMenu(menu); } 

Pay attention to two points

1- If possible, just enable or disable the menu item or see that in your case you can change the title of the same menu as it will work, because menu.clear (); handling attention may be required

2- from the link provided by Atlos

In Android 2.3.x and lower, the system calls onPrepareOptionsMenu () every time the user opens the options menu ( presses the Menu button ).

In Android 3.0 and above, the options menu is always considered open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request the onPrepareOptionsMenu () system call.

+36
source share

Here is the link: http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu

It depends on the version of Android you are targeting. Not sure if you have read this part yet, but any other advice that I have will be so verbatim.

+7
source share

All Articles