Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8131093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:58:29+00:00 2026-06-06T08:58:29+00:00

I have a menu in my app android. When I click on add favorites,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:58:32+00:00Added an answer on June 6, 2026 at 8:58 am

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I click Settings in my application's menu, app crash instantly. I don't have
I have this need to place my app in the share/send context menu so
I have an Android app where users can add items to a list, and
I have to code animated menu buttons for my Android app. It will be
I have an option menu in Android. Now it has got 5 options. When
I am making an Android app. I have a list of items displaying. I
My app has a few activities, all have an options menu, which is the
I have a menu in my Android app that holds more than 6 items
I have an Android app that uses ActionBar Sherlock. I create a menu that
I have a menu bar on my app, that I need to share between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.