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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:12:06+00:00 2026-06-11T15:12:06+00:00

In my activity’s onCreateOptionsMenu method, I am simply inflating my menu layout file and

  • 0

In my activity’s onCreateOptionsMenu method, I am simply inflating my menu layout file and attaching it.

I want to show/hide some menu items based on a value a of a global variable, I do this in the onPrepareOptionsMenu method. I’ve read that this is the correct place to do it.

My onPrepareOptionsMenu method doesn’t always fire. I don’t know why but it doesn’t always fire when I press the “Menu” button on my phone. Maybe it has some thing to do with it’s internal state.

It seems to fire when the Acitvity is being created. Pressing the “Menu” button for the first time, doesn’t cause it to fire but if I press the menu button a second time, it works just fine.

Is there a way I could force the onPrepareOptionsMenu to fire.

Thanks


Smok suggested using the invalidateOptionsMenu method to invalidate the menu items but this causes the onCreateOptionsMenu method to fire too. Here’s my method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    System.out.println("onCreate");

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();

    return true;

}

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    System.out.println("prepared");

    if (this.objAdapter == null) {
        menu.findItem(R.id.sort).setVisible(false);
        menu.findItem(R.id.filter).setVisible(false);
        menu.findItem(R.id.group).setVisible(false);
    } else {
        menu.findItem(R.id.sort).setVisible(true);
        menu.findItem(R.id.filter).setVisible(true);
        menu.findItem(R.id.group).setVisible(true);

    }

    return true;
}

As you can see from my onCreateOptionsMenu method, invoking it again will cause the focus to be lost to the SearchView.

  • 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-11T15:12:07+00:00Added an answer on June 11, 2026 at 3:12 pm

    As Smok pointed out: by calling invalidateOptionsMenu().

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

Sidebar

Related Questions

My Activity creates an AsyncTask that loads data from a file and updates the
In Activity we have method @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //... } My
Activity solution[a][b]; // ... Activity **mother = solution; I want to convert 2D array
An Activity may inflate an arbitrary layout xml that may or may not have
In Activity lifecycle the onActivityResult method is called before the onCreate methode, where all
The activity in the manifest is singleTask. The onNewIntent method is called when the
I have an Android activity which has a header, some title text, and then
My activity uses a TableView layout and I have it coded to set the
My activity's background is showing up with some horizontal lines on both emulator and
Sampling with Activity Monitor/Instruments/Shark will show stack traces full of C functions for the

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.