Trouble with actionbar and pager: options disappear

2,550 views
Skip to first unread message

Francho

unread,
Sep 29, 2011, 9:24:25 AM9/29/11
to ActionBarSherlock
I have a FragmentActivity with an ActionBarSherlock, a TabHost and one
ViewPager.

All works fine, but when I switch the page with the finger, the
options of my actionbar disappears (the logo and the actionbar remain
visible).

Any idea to solve this?

Thanks in advance

Jake Wharton

unread,
Sep 29, 2011, 2:25:38 PM9/29/11
to actionba...@googlegroups.com
The FragmentPagerAdapter will automatically hide the options menu of a fragment when it is paged away. This should also happen if you select any tab which calls setCurrentPage. Are you seeing something different? If an options menu item is relevant to all fragments it should be part of the parent activity.

---
Jake Wharton
http://about.me/jakewharton

Francho Joven

unread,
Sep 30, 2011, 2:56:08 AM9/30/11
to actionba...@googlegroups.com
The options are still part of the parent activity but the options keep disappearing.

What can I do to the adapter to keep the parent activity menu options visible?

Thanks in advance

2011/9/29 Jake Wharton <jakew...@gmail.com>

Jake Wharton

unread,
Sep 30, 2011, 9:06:14 AM9/30/11
to actionba...@googlegroups.com
Can you paste how you are adding the options items in the activity? Are you adding them in a non-standard way? It sounds like when the menu is invalidated and onCreateOptionsMenu is called again they are not being added.

Either that or provide a simple reproducible test case where I can see the error occur.

Thanks.

Francho Joven

unread,
Sep 30, 2011, 9:12:23 AM9/30/11
to actionba...@googlegroups.com
This is my activity:

public class BaseActivity extends FragmentActivity {
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_default, menu);

return super.onCreateOptionsMenu(menu);
}
...
}

and this is my menu:

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/menu_stats" android:icon="@drawable/ic_stats"
android:title="@string/menu_stats" android:orderInCategory="1"
android:showAsAction="ifRoom" />


<item android:id="@+id/menu_comparative" android:icon="@drawable/ic_comparative"
android:title="@string/menu_comparatives" android:orderInCategory="1"
android:showAsAction="ifRoom" />

<item android:id="@+id/menu_more" android:icon="@drawable/ic_menu_more"
android:title="more" android:orderInCategory="1" android:showAsAction="always">

<menu>
<item android:id="@android:id/home" android:icon="@drawable/ic_home"
android:title="home" android:orderInCategory="1" android:showAsAction="ifRoom" />
<item android:id="@+id/menu_stats2" android:icon="@drawable/ic_stats"
android:title="@string/menu_stats" android:orderInCategory="1"
android:showAsAction="ifRoom" />
<item android:id="@+id/menu_comparative2" android:icon="@drawable/ic_comparative"
android:title="@string/menu_comparatives" android:orderInCategory="1"
android:showAsAction="ifRoom" />
</menu>

</item>

</menu>

2011/9/30 Jake Wharton <jakew...@gmail.com>



--
Francho Joven
Android Developer

@.
 francho @ pocketwidget.com

Francho Joven

unread,
Oct 3, 2011, 4:28:55 AM10/3/11
to actionba...@googlegroups.com
I have just discovered that if I commented the onPageSelected method in the TabsAdapter 
(it's based on your  FragmentTabsPager sample) the menú items works fine.

Any idea?

2011/9/30 Francho Joven <fra...@pocketwidget.com>



--
Francho Joven

Leon Roberts

unread,
Oct 3, 2011, 10:49:13 AM10/3/11
to actionba...@googlegroups.com
I've seen something similar - it's intermittent otherwise I'd have a testcase for it, but occasionally the MenuItems being displayed on the action bar fail to display - the section when the icon would be (this is icon only, no text) exists but only as a black unclickable square.

In my case it works 99% of the time but now and then it suddenly fails (I think it's related to updates to the fragments being displayed - and I've only ever seen it happen during the transition to a new fragment and the corresponding update to the menus - although that might be wrong since I can't recreate it easily)  - and once it fails it's stuck failing until the activity is recreated - either through rotation or killing the app and restarting. It looks a lot like it's a z-order bug as everything seems right except it's not there.

As far as I can tell/remember only the MenuItems that have been promoted to the action bar are missing, the normal menu exists - I'd verify it now but killing and restarting the app fixed it (obviously) so I can't :(

I've added menu items programmatically but since they work 99% of the time I'd assume that they're working and the bug lies elsewhere - disturbingly this reminds me of the blank tabwidget problem I saw a while back in core android and has never been fixed.

Leon.

Jake Wharton

unread,
Oct 3, 2011, 10:59:44 AM10/3/11
to actionba...@googlegroups.com
What versions of ActionBarSherlock are you both seeing this on? There was a bug in 3.2.0, 3.2.1, and 3.2.2 which caused a race condition and could lead to either empty items or the lack of any items. Upgrading to 3.2.3 should resolve that particular bug.

In any case I can't seem to reproduce this effectively. Francho, would you be able to send me your project as a .zip directly?

Leon Roberts

unread,
Oct 3, 2011, 11:12:30 AM10/3/11
to actionba...@googlegroups.com
I've seen the problem on 3.2.3 - it's similar to the one you fixed in 3.2.2 and 3.2.3 that I mentioned when there was a bug with just using 2 fragments in the view pager - certainly the external visibility of the menuItem on the action bar is the same even though the trigger conditions are very different.

However it isn't related to the number of items, and it occurs only rarely.. It's annoying and I'd love to get it fixed, but without knowing what conditions trigger it I can't provide much help :(

Leon.

Francho Joven

unread,
Oct 4, 2011, 6:54:20 AM10/4/11
to actionba...@googlegroups.com
I'm using 3.2.3 library too.

Jake, I'm sorry but I can not share my code with you. I'm trying to generate a new proyect with only the related code, but in the new one I can't reproduce the bug

I think that is something related with the memory. I have observed that, when it 
happens, a log entry like this appears:

10-04 12:46:17.701: DEBUG/dalvikvm(96): GC_EXPLICIT freed 455K, 50% free 8040K/16071K, external 11648K/13696K, paused 285ms

Regards

2011/10/3 Leon Roberts <zeeto...@gmail.com>

I've seen the problem on 3.2.3 - it's similar to the one you fixed in 3.2.2 and 3.2.3 that I mentioned when there was a bug with just using 2 fragments in the view pager - certainly the external visibility of the menuItem on the action bar is the same even though the trigger conditions are very different.

However it isn't related to the number of items, and it occurs only rarely.. It's annoying and I'd love to get it fixed, but without knowing what conditions trigger it I can't provide much help :(

Leon.
--

Francho Joven

unread,
Oct 4, 2011, 7:21:40 AM10/4/11
to actionba...@googlegroups.com
Another clue?: I'm have just test my app in a Samsung Galaxy Tab (Android 3.0.1) and the bug is present too.

2011/10/4 Francho Joven <fra...@pocketwidget.com>

Francho Joven

unread,
Oct 4, 2011, 10:01:34 AM10/4/11
to actionba...@googlegroups.com
more clues: When the options are dissapeared, if I perform a long click on the menu buttom, it appears again (and the soft keyboard) :-/



2011/10/4 Francho Joven <fra...@pocketwidget.com>

Jake Wharton

unread,
Oct 5, 2011, 3:08:49 PM10/5/11
to actionba...@googlegroups.com
No worries. I understand a lot people are using this with internal and closed-source projects.

I can't seem reproduce this. The fact that it occurs on the Honeycomb tablet points to a problem in either FragmentActivity handling the menus or just some odd race-condition with your implementation.

If you can, make a full copy of your project and then gut it in steps (ensuring the bug still presents itself after each step) until you can get it down to as minimal of a test case as possible that is free of any information specific to your app or company. I would really like to make sure this isn't a bug in the library itself.

Miles

unread,
Oct 9, 2011, 11:13:51 AM10/9/11
to ActionBarSherlock
I must admit I've seen this issue myself. I'd agree with Jake, looks
to be some sort of race condition.

My use case is as follows:
FragmentActivityLoads up with a FragmentPager, TitlePageIndicator and
a single Fragment. The menu shows up.

A async method completes and I add several additional fragments to the
pager. The menu disappears.

If I swipe to the first page, the menu reappears.
If I reorient the screen, the menu appears.

My original fix would be to reassociate the pager with the adapter but
that doesn't seem to work these days.

I have noticed that onPageSelected (associated with the
TitlePageIndicator) isn't fired (even if you programmatically select a
page).

I'll keep investigating.

On Oct 5, 8:08 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> No worries. I understand a lot people are using this with internal and
> closed-source projects.
>
> I can't seem reproduce this. The fact that it occurs on the Honeycomb tablet
> points to a problem in either FragmentActivity handling the menus or just
> some odd race-condition with your implementation.
>
> If you can, make a full copy of your project and then gut it in steps
> (ensuring the bug still presents itself after each step) until you can get
> it down to as minimal of a test case as possible that is free of any
> information specific to your app or company. I would really like to make
> sure this isn't a bug in the library itself.
>
> ---
> Jake Whartonhttp://about.me/jakewharton
>
> On Tue, Oct 4, 2011 at 10:01 AM, Francho Joven <fran...@pocketwidget.com>wrote:
>
>
>
>
>
>
>
> > more clues: When the options are dissapeared, if I perform a long click on
> > the menu buttom, it appears again (and the soft keyboard) :-/
>
> > 2011/10/4 Francho Joven <fran...@pocketwidget.com>
>
> >> Another clue?: I'm have just test my app in a Samsung Galaxy Tab (Android
> >> 3.0.1) and the bug is present too.
>
> >> 2011/10/4 Francho Joven <fran...@pocketwidget.com>
>
> >>> I'm using 3.2.3 library too.
>
> >>> Jake, I'm sorry but I can not share my code with you. I'm trying to
> >>> generate a new proyect with only the related code, but in the new one I
> >>> can't reproduce the bug
>
> >>> I think that is something related with the memory. I have observed that,
> >>> when it
> >>> happens, a log entry like this appears:
>
> >>> 10-04 12:46:17.701: DEBUG/dalvikvm(96): GC_EXPLICIT freed 455K, 50% free
> >>> 8040K/16071K, external 11648K/13696K, paused 285ms
>
> >>> Regards
>
> >>> 2011/10/3 Leon Roberts <zeetoo....@gmail.com>
>
> >>>> I've seen the problem on 3.2.3 - it's similar to the one you fixed in
> >>>> 3.2.2 and 3.2.3 that I mentioned when there was a bug with just using 2
> >>>> fragments in the view pager - certainly the external visibility of the
> >>>> menuItem on the action bar is the same even though the trigger conditions
> >>>> are very different.
>
> >>>> However it isn't related to the number of items, and it occurs only
> >>>> rarely.. It's annoying and I'd love to get it fixed, but without knowing
> >>>> what conditions trigger it I can't provide much help :(
>
> >>>> Leon.
>
> >>> --
> >>> *Francho Joven*
> >>> Android Developer
> >>> *
> >>> *
> >>> @*.* *francho @ **po**cket**wid**get**.com*
>
> >> --
> >> *Francho Joven*
> >> Android Developer
> >> *
> >> *
> >> @*.* *francho @ **po**cket**wid**get**.com*
>
> > --
> > *Francho Joven*
> > Android Developer
> > *
> > *
> > @*.* *francho @ **po**cket**wid**get**.com*

Francho Joven

unread,
Oct 11, 2011, 9:59:19 AM10/11/11
to actionba...@googlegroups.com
With this project I had reproduced the error sometimes (when I slide from tab2 to tab1).

I think that is something related with an async thing (because the error appears if a fragment is more slow than other)



 
2011/10/9 Miles <miles....@gmail.com>
ActionbarTabPagerExperiment.zip

Francho Joven

unread,
Oct 19, 2011, 8:07:08 AM10/19/11
to actionba...@googlegroups.com
Any news about this issue?

Regards

2011/10/11 Francho Joven <fra...@pocketwidget.com>

Leonardo Martins Scaramel

unread,
Oct 19, 2011, 10:45:27 AM10/19/11
to actionba...@googlegroups.com
Hi,

I have found the problem. It was the library. I just removed it, downloaded again and works!

thanks!



2011/10/19 Francho Joven <fra...@pocketwidget.com>



--
Leonardo Martins Scaramel
MSN: xx11_l...@hotmail.com
Fone Cel: 5475-9955

"Até Mais e Obrigado pelos Peixes"

Francho Joven

unread,
Oct 20, 2011, 3:10:01 AM10/20/11
to actionba...@googlegroups.com
I have just upgrade SherlockActionbar to 3.3.1, I cleaned and rebuilt my proyect but 
the bug is still here.

It's necesary do some changes in my code?

Regards

2011/10/19 Leonardo Martins Scaramel <leonard...@gmail.com>

Hi,

I have found the problem. It was the library. I just removed it, downloaded again and works!

thanks!

Jake Wharton

unread,
Oct 24, 2011, 11:49:19 PM10/24/11
to actionba...@googlegroups.com
There was a bug in that onPrepareOptionsMenu was not being called on pre-3.0 when action items were present. It should be fixed in the dev branch if you wanted to test.

Francho Joven

unread,
Oct 25, 2011, 2:12:11 AM10/25/11
to actionba...@googlegroups.com
Ok. I'll test it.

When think you that this fugfixed will be added to the production branch?

Thanks for all.

2011/10/25 Jake Wharton <jakew...@gmail.com>

Jake Wharton

unread,
Oct 25, 2011, 7:08:51 AM10/25/11
to actionba...@googlegroups.com
Within a week if no major problems are found.

nhasan

unread,
Jan 17, 2012, 4:49:04 PM1/17/12
to actionba...@googlegroups.com
Jake,

I have all the same symptoms and behavior described in this thread.

* I am using ABS 3.5.1 with ActionBar/Tabs/ViewPager modeled after the tabs pager sample.
* I have two fragments, with one menu item (search) as action item.
* The search icon shows up initially but disappears once I swipe and does not come back even if I swipe back.
* If I long press the menu key, it comes back
* If I comment the contents of onPageSelected(), everything works fine but of course the tabs are not updated on swipe.
* If I move the menu from fragments to the activity, everything works fine.

Looks like this is only a problem with menus defined in fragments. This is driving me nuts here. Please let me know if I can provide more info. My app (FlightIntel for Pilots) is open source and hosted on github but these changes are not there yet.

Thanks a lot for your work on this library.

Message has been deleted

Kekec

unread,
Aug 21, 2012, 5:52:33 AM8/21/12
to actionba...@googlegroups.com, nha...@nadmm.com
I have the same issue as described in the last post before me. I am using latest version 4.1. When swipe between fragments actionbar icons are not visible, but when i click on tab to change Fragment they show normally.
The sample project "ABS: Fragments" works normally, i also compared source code to my project but can't find what i did wrong.

Here is code where i add the actionbar items:
        @Override 
          public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
              MenuItem populateItem = menu.add(Menu.NONE, 1, 0, "Test1");
              populateItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
              MenuItem clearItem = menu.add(Menu.NONE, 2, 0, "Test2");
              clearItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
          }  

I would be really happy if someone can give me some tip to this problem.
Thanks.

Dne torek, 17. januar 2012 22:49:04 UTC+1 je oseba nhasan napisala:

Sébastien Billion

unread,
Sep 3, 2012, 6:56:37 AM9/3/12
to actionba...@googlegroups.com
I had the same problem and I resolved it by define onCreateOptionsMenu in the MainFragment which manage the tabsPager. When an item action can be share by all fragments, just do like that.
In fragments, you can Override onOptionsItemSelected.
If you need to add specific action item for each fragment, use onPrepareOptionMenu.
I hope that will help you. It works for me but I can't ensure it will be your case.

Best regards,
Seb

2012/8/21 Kekec <android.p...@gmail.com>

Raphael Ackermann

unread,
Sep 6, 2012, 4:05:08 AM9/6/12
to actionba...@googlegroups.com
I also had the same problem with ABS 4.1

I had onCreateOptionsMenu both in the FragmentTabsPager and in the ListFragement. In the main fragment I would add the general menu action and in the list fragment the ones specific to the tab.

When removing onCreateOptionsMenu from the FragmentTabsPager the issue went from happens after 3 swipes to happens after about 10 or more swipes.

Commenting out 
@Override
public void onPageSelected(int position) {
// Unfortunately when TabHost changes the current tab, it kindly
// also takes care of putting focus on it when not in touch mode.
// The jerk.
// This hack tries to prevent this from pulling focus out of our
// ViewPager.
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}

leads to the problem going away, but obviously then the currently selected tab is not indicated.

What I also noticed is that the longer the fragment takes to load (e.g. more images/longer list) the more likely it is that the action menus will not be shown.

Raphael

Raphael Ackermann

unread,
Sep 6, 2012, 4:24:57 AM9/6/12
to actionba...@googlegroups.com
Hi Jake

This can be reproduced in the ABS Fragments sample application that can downloaded from your website.

To reproduce:
1. Make sure that the fragments don't cache any data. e.g. uninstall the app.
2. Open the app and select Tabs and Pager from the list.
3. Once it opens, quickly swipe to the Throttle Tab at the very right and then go back to the Custom Tab next to it. 
4. Observe that the Custom Tab does not show the Search menu action.
5. Swiping now lef or right and going back to the custom tab will always make the search icon appear.

I've managed to reproduce this twice now in 3 tries.  Key is to be fast in swiping to Throttle and back. And it only ever works if the Custom fragment is not loaded and does not cache it's data.

I've attached screenshots of the Custom Tab with and without Search action menu.

Hope that this will help in tracking down the root cause.

Raphael
Screenshot_2012-09-06-10-16-40.png
Screenshot_2012-09-06-10-17-18.png

Jake Wharton

unread,
Sep 6, 2012, 1:07:39 PM9/6/12
to actionba...@googlegroups.com
This is a bug in the support library/framework interaction, not ABS. http://b.android.com/29472
Reply all
Reply to author
Forward
0 new messages