Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reload menu android in the same activity?

I have a menu in my app android. When I click on add favorites, I need to reload the menu options, making it appear del favorites in the options and not appear add favorites.

I don't want use reload activity because of the back button.

My code:

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!

like image 513
Fabiano Palaoro Avatar asked Jun 25 '12 18:06

Fabiano Palaoro


2 Answers

use onPrepareOptionsMenu

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

@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);
}

Please note two points

1- If possible just enable or disable menu item or looks possible in your case can change the title of same menu will work because menu.clear(); may need over attention while handling

2- as per link provided by Atlos

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

On Android 3.0 and higher, the options menu is considered to always be 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 that the system call onPrepareOptionsMenu().

like image 145
Dheeresh Singh Avatar answered Nov 08 '22 11:11

Dheeresh Singh


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

It depends what version of Android you are targeting. Not sure if you have read this part yet, but any other advice I have would be this verbatim.

like image 32
telkins Avatar answered Nov 08 '22 12:11

telkins