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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:01:26+00:00 2026-06-14T02:01:26+00:00

I am having trouble getting the following piece of code to work out. I

  • 0

I am having trouble getting the following piece of code to work out. I have a viewpager with 3 fragments, and I want a search icon to only show up on one. I started off trying to add the search function by the fragment, but the rendering of the menu item was slow when swiping to that page. I am now on the part to add the search icon to the activity, and then just hide or show depending on which viewpager page is active, but the following is not working:

public class MyApp extends FragmentActivity implements 
   FragmentTeams.FragmentNotification,ViewPager.OnPageChangeListener, 
      OnNavigationListener{

  ...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);

    menuSearch = menu.findItem(R.id.menu_search);
    mSearchView = new SearchView(this);
    menuSearch.setActionView(mSearchView);
    menuSearch.setVisible(false);
    return true;
}

 @Override
public void onPageSelected(int pageNum) {

if(pageNum== 1){     
    ActionBar actionBar = MyApp.this.getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);     
        menuSearch.setVisible(true);
        invalidateOptionsMenu();

}else{           
    ActionBar actionBar = MyApp.this.getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
        menuSearch.setVisible(false);
        invalidateOptionsMenu();
    }
}

While the above does (appear to) create and hide the icon at onCreateOptionsMenu, it is not reenabled when moving to

 pageNum ==1  

Can anyone give me some insight as to why this may be happening?

  • 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-14T02:01:27+00:00Added an answer on June 14, 2026 at 2:01 am

    invalidateOptionsMenu make the system calls the method onPrepareOptionsMenu, so you can override this method as follows:

    public boolean onPrepareOptionsMenu(Menu menu) {
          int pageNum = getCurrentPage();
          if (pageNum == 1) {
             menu.findItem(R.id.menu_search).setVisible(true);
    
          }
          else {
             menu.findItem(R.id.menu_search).setVisible(false);
          }
       }
    
    public void onPageSelected(int pageNum) {
        invalidateOptionsMenu();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble getting the following code to work. Calling swapLevel() directly works fine, but
I am having trouble getting the following code to work: function doGet () {
I am having trouble getting the bulk uploader to work. I have been following
Having trouble getting the following code to work: $('#changeMode').button().click(function(){ $('#playfield').toggle(function() { $(this).switchClass(gridView,plainView); }, function()
I am having trouble getting the following code to work in FireFox (3.6.4), however,
I have been having some trouble getting the following to work: <IfModule mod_rewrite.c> DirectoryIndex
I'm having trouble getting the following code to work correctly. Using an online IEEE-754
I'm having trouble getting my delegate method to fire. I have the following code:
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
I'm having trouble getting the following three-column layout to work: A B C +-------+-------------------+------------+

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.