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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:18:35+00:00 2026-05-27T00:18:35+00:00

I’m building an Android Application (minimum SDK Level 10, Gingerbread 2.3.3) with some Fragments

  • 0

I’m building an Android Application (minimum SDK Level 10, Gingerbread 2.3.3) with some Fragments in a ViewPager. I’m using ActionBarSherlock to create an ActionBar and android-viewpagertabs to add tabs to the ViewPager just like in the Market client.

I have one global menu item that I want to be shown on every tab/fragment. On the first of the three tabs I want to have two additional menu items.

But now two strange things happen:

First if I start the app, everything seems to be fine, I can see all three menu items on the first page and only one item if i swipe to the second and third tab. But if I swipe back to the second tab from the third one, I can see all three items again which shouldn’t happen. If I swipe back to the first and then again to the second tab, everything is fine again.

The other strange thing is that every time I rotate the device, the menu items from the fragment are added again, even though they are already in the menu.

Code of the FragmentActivity that displays the ViewPager and its tabs:

public class MainActivity extends FragmentActivity {

    public static final String TAG = "MainActivity";

    private ActionBar actionBar;
    private Adapter adapter;
    private ViewPager viewPager;
    private ViewPagerTabs tabs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.volksempfaenger);

        actionBar = getSupportActionBar();

        adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(getString(R.string.title_tab_subscriptions),
                SubscriptionGridFragment.class);
        // adding more fragments here

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);

        tabs = (ViewPagerTabs) findViewById(R.id.tabs);
        tabs.setViewPager(viewPager);
    }

    public static class Adapter extends FragmentPagerAdapter implements
            ViewPagerTabProvider {

        private FragmentManager fragmentManager;
        private ArrayList<Class<? extends Fragment>> fragments;
        private ArrayList<String> titles;

        public Adapter(FragmentManager fm) {
            super(fm);
            fragmentManager = fm;
            fragments = new ArrayList<Class<? extends Fragment>>();
            titles = new ArrayList<String>();
        }

        public void addFragment(String title, Class<? extends Fragment> fragment) {
            titles.add(title);
            fragments.add(fragment);
        }

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

        public String getTitle(int position) {
            return titles.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            try {
                return fragments.get(position).newInstance();
            } catch (InstantiationException e) {
                Log.wtf(TAG, e);
            } catch (IllegalAccessException e) {
                Log.wtf(TAG, e);
            }
            return null;
        }

        @Override
        public Object instantiateItem(View container, int position) {
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            Fragment f = getItem(position);
            fragmentTransaction.add(container.getId(), f);
            fragmentTransaction.commit();
            return f;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        BaseActivity.addGlobalMenu(this, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return BaseActivity.handleGlobalMenu(this, item);
    }

}

Code of the fragment that shall have its own menu items:

public class SubscriptionGridFragment extends Fragment {

    private GridView subscriptionList;
    private SubscriptionListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    // ...

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // ...
    }
}
  • 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-27T00:18:36+00:00Added an answer on May 27, 2026 at 12:18 am

    Thanks to SimonVT from freenode/#android-dev: “protip: Look at what fragmentpageradapter actually does before you override instantiateItem”

    Removing this method solved the whole problem.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.