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!
android android-menu
Fabiano palaoro
source share