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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:50:34+00:00 2026-05-31T12:50:34+00:00

I have an activity that uses a fragmentpageradapter to create an ics style actionBar.

  • 0

I have an activity that uses a fragmentpageradapter to create an ics style actionBar. Each page needs to update the actionBar though. Is there a way I can call onCreateOptionsMenu in my onPageSelected?

I’ve trimmed a lot of the code out from the example below for simplicity’s sake.

public class ListFragmentViewPagerActivity extends FragmentActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thread_view);

        ViewPager pager = (ViewPager) findViewById(android.R.id.list);
        pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager()));
        TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);
        indicator.setOnPageChangeListener(new OnPageChangeListener() {
             @Override
             public void onPageSelected(int position) {

                 }

            @Override
            public void onPageScrollStateChanged(int arg0) {                
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {                
            }
        });
    }

    public class ExamplePagerAdapter extends FragmentPagerAdapter implements TitleProvider{

        public ExamplePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return URLS.size();
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new ThreadFragment();

            // set arguments here, if required
            Bundle args = new Bundle();
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public String getTitle(int pos) {
            return TITLES.get(pos);
        }

    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuinflate = new MenuInflater(this);
        menuinflate.inflate(R.menu.thread_menu, menu);
        if (type.equals("xda")) {
            menu.removeItem(R.id.ss_view);
        }

            //This worked when I only needed to call it one time. I need to update this menu for each page in my viewPager though.
        if (isFav) {
            menu.getItem(2).setIcon(R.drawable.fav_ab);
        }
        return true;        
    }   

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            break;
            case R.id.ss_view:
                Intent ssi = new Intent(this, SSActivity.class);
                ssi.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Bundle b = new Bundle();
                ssi.putExtras(b);
                startActivity(ssi);
            break;
            case R.id.restart:
            break;
            case R.id.fav_ab:
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return false;
    }    
}

UPDATE
Calling invalidateOptionsMenu() in my onPageSelected() did the trick!

  • 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-05-31T12:50:35+00:00Added an answer on May 31, 2026 at 12:50 pm

    Use invalidateOptionsMenu() but make sure you wrap this in a trycatch if you are supporting anythign below 3.0, as this method does not exist and will crash!

    If you want to update your menu on pre 3.0 devices, override the onPrepareOptionsMenu() as well, which will be called everytime the menu is opened.

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

Sidebar

Related Questions

I have an activity that has the following set as theme: android:theme=@android:style/Theme.Dialog However, there
I have a simple Activity that uses a android:theme=@android:style/Theme.Dialog in the manifest. My activity
I have a ViewPager that uses ListFragments on each page. I implement onListItemClick and
I have an app that uses the Graph API to post activity to a
I have an Activity that uses AsnycTasks to reset and restart a MediaPlayer after
I have a single activity app that uses a PagerAdapter. In the OnCreate event
The scenario is the following: I have an activity RunTrainingWorkoutsView that uses XML layout
I have an activity configured with splitActionBarWhenNarrow that uses requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY) . And I need
I have an Android activity utilizing a JNI library that uses netlink commands to
I have a table called activity that uses the following columns: LoginDate, LoginID, Department,

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.