Julian
Julian

Reputation: 2101

Duplication of menu items with ViewPager and Fragments

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) {
        // ...
    }
}

Upvotes: 2

Views: 4623

Answers (3)

passsy
passsy

Reputation: 5222

Had the same problem.

My mistake was that I created a new Viewpager in oncreate and set as id

viewpager.setId(viewpager.hashcode());
setContentView(viewpager);

But that was a big mistake, beacause after an orientation change the FragmentPagerAdapter tries to reuse the old Fragments. It looks in the FragmentManager for a Fragment with an id generated from the viewpagers id and the fragments position.

public Object instantiateItem(ViewGroup container, int position) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }

        final long itemId = getItemId(position);

        // Do we already have this fragment?
        String name = makeFragmentName(container.getId(), itemId);
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        if (fragment != null) {
            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
            mCurTransaction.attach(fragment);
        } else {
            fragment = getItem(position);

container is the Viewpager.

Solution:

viewpager.setId(R.id.viewpager);
setContentView(viewpager);

Upvotes: 1

dbDev
dbDev

Reputation: 1549

Found this in an Android sample project. In the fragment activity, the onCreate needs to check to see if savedInstanceState is still active, like this:

    setContentView(R.layout.custom_detail_fragment);

    if (savedInstanceState != null) {
        return;
    }

    CDFrag = new CustomDetailFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.custom_detail_fragment_container, CDFrag).commit();

The key is this part:

    if (savedInstanceState != null) {
        return;
    }

I was also getting the duplicate items in my action bar, when I rotated and once I added that check, the duplication stopped.

Upvotes: 1

Julian
Julian

Reputation: 2101

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.

Upvotes: 0

Related Questions