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 8395597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:20:30+00:00 2026-06-09T20:20:30+00:00

I want to refresh the option menu each time it is called, I have

  • 0

I want to refresh the option menu each time it is called,

I have a functionality in which when the user clicks the option(add/remove favourite) in option menu, it checks whether it is favourite or not and do the functionality,

Problem : Once it creates the menu , it does not refresh the onCreateOptionMenu on 2nd time user presses the option button. I want to refresh the optionMenu each time it is pressed. Here is my code for this

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    if (is_favorite.equals("1")) {
        menu.add(1, 22 ,0,"Remove from Favourites").setIcon(R.drawable.favorites_unselected);

    }
    else{
        menu.add(1, 11 ,0,"Add to Favourites").setIcon(R.drawable.favorites_selected);
    }
    return true;
}
@Override
public boolean onPrepareOptionsMenu (Menu menu)
  {

    return super.onPrepareOptionsMenu(menu);
  }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {


    case 11:            
            //addtofavouritestask
            is_favorite = "1";

        return true;
    case 22:
        //removeFromFavouritestask
            is_favorite = "0";

        return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
  • 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-09T20:20:32+00:00Added an answer on June 9, 2026 at 8:20 pm

    According to the doc, onCreateOptionMenu (Menu menu) is called only once time, so it does not refresh your menu. You must call the method onPrepareOptionsMenu(Menu) to refresh it (” To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu)“).

    So this should work :

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        if (is_favorite.equals("1")) {
            menu.add(1, 22 ,0,"Remove from Favourites").setIcon(R.drawable.favorites_unselected);
    
        }
        else{
            menu.add(1, 11 ,0,"Add to Favourites").setIcon(R.drawable.favorites_selected);
        }
        return true;
    }
    @Override
    public boolean onPrepareOptionsMenu (Menu menu)
      { 
    menu.removeGroup(1);
    if (is_favorite.equals("1")) {
            menu.add(1, 22 ,0,"Remove from Favourites").setIcon(R.drawable.favorites_unselected);
    
        }
        else{
            menu.add(1, 11 ,0,"Add to Favourites").setIcon(R.drawable.favorites_selected);
        }
        return super.onPrepareOptionsMenu(menu);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to refresh a table using a SSIS package every time the user
I have couple of formulas and data coming from database. I want to refresh
want to open pdf file when a user clicks on hyperlink shown in gridview
I want to make a interactive uploading option like Facebook and gmail have where
I have a ListView bound to a SimpleCursorAdapter, and I want it to refresh
I have a rich:extendedDataTable inside a rich:tab . I want to refresh the table
I have A game to which I recently added a global high score functionality
NO JQUERY! I want to be able to refresh the page and have the
I want to refresh Google Map and send new geolocalization request without refreshing the
I want to submit a form without page refresh using jQuery. I found some

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.