Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Duplicated menu items in the action bar #460

waninkoko opened this Issue May 8, 2012 · 10 comments


None yet
4 participants


I have a problem related with duplicated menu items in the action bar.

I have a ViewPager in my activity that contains 3 different fragments. These fragmets create their own menu items and, of course, they call "hasMenuOptions(true)" in onCreate().

The first time I launch the application everything is fine. When I swipe the menu items are hidden or shown depending of the fragment that's shown. Everything works ok.

The problem appears when I move the task to background (for example, pressing the HOME button) and I start doing stuff until the activity is "stopped" (or destroyed, or whatever). When I come back to the application through the system notification I create, a new activity is created (as expected) correctly BUT the menu items from the fragments appear duplicated. If I repeat this pattern, they are duplicated again!

This problem does NOT happen if I move the task to background and I come back to it before the activity gets detroyed (in that case I just return to the activity, in the same state I left it and without any menu items duplicated).

I also posted this problem in stackoverflow but at the moment I didn't receive a solution for this: http://stackoverflow.com/questions/10485028/fragmentstatepageadapter-duplicates-actionbar-menu-items

This happens in Gingerbread and ICS (using the support library v4 and actionbarsherlock).

Any ideas?


iNoles commented May 8, 2012

I am curious about mFragments.put(position, fragment);

That's just a HashMap to store the created fragments (and return them if the ViewPager calls getItem(), to avoid a new instantiation of the Fragment). Anyway, this also happens without it. And ONLY happens when the activity is restarted (I know it gets restarted because, when I enter the application through the notification, onCreate() gets called... so mFragments will be empty at the beginning).

This happens when I use FragmentStatePagerAdapter as adapter. When I use FragmentPagerAdapter, everything works ok too, but when the activity gets restarted the menu items are not duplicated but the fragments does NOT show anymore.

If I use one I have a problem, if I use the another I have a different problem. This is a big headache for me right now.

iNoles commented May 8, 2012

in http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html, This class also handles saving and restoring of fragment's state. It is going to be hard to pinpoint problems without full code for FragmentPagerAdapter or FragmentStatePagerAdapter.

I checked the code of that class in the v4 support library, and the problems occurs when it adds the fragments with the fragment manager.

FragmentPagerAdapter checks if the fragment already exists in the fragment manager. If it exists, it just attaches the fragment, if not it asks to create the fragment and adds it. When it has to destroy the pages of the ViewPager, it just deattaches the fragments.

FragmentStatePagerAdapter always creates a new fragment and adds it to the fragment manager. At first glance I thought it could be adding duplicated fragments, but I checked that old fragments are removed when the ViewPager are destroyed. Also, this class has an array called "mFragments" (yeah, like the HashMap I declared in my code, and it works in a similar way too, but I cannot use this array directly as it's declared private) that stores the created fragments and returns them instead of re-creating them (but, when the activity is restarted, this array is empty as the adapter was destroyed and we're working with a new instance of it).

If I modify the original FragmentPagerAdapter to always add/remove fragments (instead of adding and then attaching/dettaching), like FragmentStatePagerAdapter does, the same problem of duplicated menu items happens (so it's not a problem about saving and/or restoring the ViewPager state).

I also checked that, when a fragment is added using the fragment manager, it calls a function called 'dispatchOnCreateMenuOptions()' that just checks which added fragments have the mHasMenu flag enabled and then calls 'onCreateOptionsMenu()' in each one.

I don't know if I made a mistake in my code (I checked it dozens of times) or it's some kind of bug in the v4 support library. I have to check if this happens without the support lib (but, in that case, my app would only work in HC and ICS).

I forgot to say that I use singleTask launch mode (and this happens too with singleTop). If I use standard launch mode, it works ok.

If I override onSaveInstanceState() in my activity, and I don't call super.onSaveInstanceState(), the menu items are not duplicated anymore.


JakeWharton commented May 9, 2012

That's because not calling the superclass method doesn't allow the fragment manager to save its state. This is probably just an interaction bug since fragment management and state saving has nothing to do with ActionBarSherlock itself.


Now I'll have to find a way to fix this as I already know what happens.

Thanks for your help!

I have the same issue yet, in the last version. I don't know exactly in which situation it occurs, but sometimes I have menu items duplicated. I solved like this: http://stackoverflow.com/a/8495697/450148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment