ActionbarSherlock and ViewpagerIndicator: how to add another action menu to another page in ViewPagerIndicator using fragment

My application is using Actionbarsherlockc ViewpagerIndicator. Each page uses 1 fragment. I want each page to have a different action menu, so how can I archive this?

I tried to use onCreateOptionsMenufor each SherlockFragment, but the menu of the action bar did not appear, I also tried to find some methods for adding the menu programmatically, but in no way.

@Override
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater inflater)
{       
    inflater.inflate(R.menu.main, menu);    
    super.onCreateOptionsMenu(menu, inflater);
}

Thanks you!

* EDIT: add more details *

If I use onCreateOptionsMenu in Activity, the menu (action item) is usually displayed.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getSupportMenuInflater();
    menuInflater.inflate(R.menu.main, menu);

This is how I set the ActionBar in the ViewPager

final ActionBar actionbar = getSupportActionBar();
    actionbar .setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionbar .setDisplayHomeAsUpEnabled(true);
    actionbar .setTitle("olala");

    mFragmentAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());     
    mPager.setAdapter(mFragmentAdapter);

    mIndicator.setViewPager(mPager);
    mIndicator.setOnPageChangeListener(new MyPageChangeListener());
    mPager.setCurrentItem(3);
+3
2

Fragment (onCreate onCreateView), , , onCreateOptionsMenu (Menu, MenuInflater) .

setHasOptionsMenu(true);

ref: http://developer.android.com/reference/android/app/Fragment.html

+5

super.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.chat_thread_list_fragment_menu, menu);
}

Update:

onCreate of Activity...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_activity);
    final ViewPager viewPager = (ViewPager) findViewById(R.id.profile_activity_pager);

    final Intent intent = getIntent();

    // I guess some setup should be done...
    final ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    bar.setDisplayHomeAsUpEnabled(true);
    if (intent.hasExtra(EXTRA_USER_NAME)) {
        bar.setTitle(intent.getStringExtra(EXTRA_USER_NAME));
    } else {
        bar.setTitle(R.string.title_profile);
    }

    final PagerAdapter tabsAdapter = new TabsAdapter(this);
    viewPager.setAdapter(tabsAdapter);

    final TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.profile_activity_tabs);
    titleIndicator.setViewPager(viewPager);

    if (intent.hasExtra(EXTRA_TAB_ID)) {
        final int tabId = intent.getIntExtra(EXTRA_TAB_ID, -1);
        if (tabId > -1 && tabId < tabsAdapter.getCount()) {
            viewPager.setCurrentItem(tabId);
        }
    }
}
+1

All Articles