I'm using ActionBarSherlock, and my main activity has a ViewPager with three pages, all of them fragments. One of those fragments has an onCreateOptionsMenu
method, and the action bar in the main activity is updated correctly.. sort of. Here's the method:
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.search, menu);
// A couple of other things
}
I'm using a custom FragmentPagerAdapter
:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
FragmentManager fm;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
accountFragment = Account.newInstance();
return accountFragment;
case 1:
filesFragment = Files.newInstance();
return filesFragment;
case 2:
transfersFragment = Transfers.newInstance();
return transfersFragment;
}
return null;
}
private String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return titleAccount;
case 1:
return titleFiles;
case 2:
return titleTransfers;
}
return null;
}
}
I have no code at all in my activity that calls supportInvalidateOptionsMenu
, so I assume the framework is checking the fragments' options menus for me, but I don't like how it does it. Here's what I mean:
Stock apps (Phone, People, etc) as well as some other apps do this correctly. That is, as soon as I fling to a page, that new page's menu items are displayed. But in mine, they are only displayed at the end of the swiping animation.
Here's something I've tried:
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
supportInvalidateOptionsMenu();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageScrollStateChanged(int arg0) {}
});
This seems to do nothing.
Logging shows that onPageSelected
is called right when I swipe to another page, which is when I want the items to update. Logging in my fragment's onCreateOptionsMenu
shows that it is called only at the end of the animation, which has a considerable delay from when onPageSelected
is called.
How do I fix this?