Home » Android » android – How to use Actionbar items with Viewpager + Fragments

android – How to use Actionbar items with Viewpager + Fragments

Posted by: admin June 15, 2020 Leave a comment


Im trying to figure out how to implement Actionbar items based on the Fragment that is being loaded inside my viewpager. My code is below and im just trying to figure out the best practice for working with ABS + ViewPager + Fragments.


public class MainActivity extends SherlockFragmentActivity {
private ViewPager mPager;
private TitlePageIndicator mIndicator;
private MainPagerAdapter mAdapter;
private List<Fragment> mFragments;

private static final String FRAGMENT1 = Fragment1.class.getName();
private static final String FRAGMENT2 = Fragment2.class.getName();
private static final String FRAGMENT3 = SupportFragment.class.getName();

public void onCreate(Bundle savedInstanceState) {

    // add fragments
    mFragments = new ArrayList<Fragment>();
    mFragments.add(Fragment.instantiate(this, FRAGMENT1));
    mFragments.add(Fragment.instantiate(this, FRAGMENT2));
    mFragments.add(Fragment.instantiate(this, FRAGMENT3));

    // adapter
    mAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);

    // pager
    mPager = (ViewPager) findViewById(R.id.view_pager);

    // indicator
    mIndicator = (TitlePageIndicator) findViewById(R.id.title_indicator);


public class MainPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments;
private String[] titles = new String[] {"System", "Community", "Support"};
private int mCount = titles.length;

public MainPagerAdapter(FragmentManager fm, List<Fragment> f) {
    mFragments = f;

public Fragment getItem(int position) {
    return mFragments.get(position);

public int getCount() {
    return mCount;
public CharSequence getPageTitle(int position) {
    return titles[position];


 public class Fragment1 extends SherlockFragment {
WebView web;
static ProgressBar progressBar;

public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
    View v = inf.inflate(R.layout.activity_main, grp, false);
    web = (WebView) v.findViewById(R.id.webView);
    progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
    return v;
public void onActivityCreated(Bundle savedInstanceState) {
    web.setWebViewClient(new SpecialWebView(progressBar));
    web.setWebChromeClient(new SpecialWebChromeClient(progressBar));
    // Progress Bar - End

    WebSettings websettings = web.getSettings();

Inside my MainActivity i’ve tried the following and cant get the code right.

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater Inflater = getSupportMenuInflater();
    Inflater.inflate(R.menu.menu, menu);
    return true;

public boolean onOptionsItemSelected(MenuItem Item) {
    final int current = ((ViewPager) mFragments).getCurrentItem();
    String currentId = current == 0 ? FRAGMENT1: (current == 1 ? FRAGMENT2: FRAGMENT3);
    //WebView web = (WebView) findViewById(currentId);
    switch (Item.getItemId()) {
    case R.id.menu_back:
    //  web.goBack();
        return true;
    case R.id.menu_forward:
        return true;
    case R.id.menu_refresh:
        return true;
    return super.onOptionsItemSelected(Item);
How to&Answers:

Im trying to figure out how to implement Actionbar items based on the Fragment that is being loaded inside my viewpager.

That happens automatically. Just have the fragments call setHasOptionsMenu(true), then implement onCreateOptionsMenu() and onOptionsItemSelected(). The Fragment*PagerAdapter will arrange to repopulate the action bar after a page change, to remove action bar items from the previous page and introduce action bar items from the newly-current page.

Here is a sample project demonstrating having action bar items come and go, as only odd-numbered pages (for 1-based indexes, what the user sees) have action bar items.