9

I have an app with multiple tabs in an Action Bar, and each tab has a different set of option menu icons that show up in the Action Bar. Some tabs have two icons, some have none.

The problem is that when I swipe between the screens, the buttons for the individual screens may or may not show up. I.e. on a screen where there should be two icons, there will be no icons in the Action Bar!

When I click the tabs, however, the screens always show the correct icons without fail.

I have tried analyzing the problem in the debugger. I'm certain that the FragmentPagerAdapter is correctly calling setMenuVisibility(true) for the newly selected Fragment, and other Fragments are called with setMenuVisibility(false).

I can't find an exact pattern to the appearance/disappearance of buttons. I can swipe left/right between two screens that each have two different buttons. The problem will occur anywhere after 1 to 20 swipes.

The disappearance seems to be aggravated by swiping to a screen with one icon and then back to a screen with two icons.

I have tried adding calls into the Activity.invalidateOptionsMenu() but it seems to have no affect. For example, I added this call into my TabAdapter's onPageScrollStateChanged() which is called after the swipe animation is complete. I've also tried adding this into the Fragments' onResume() or after TabAdapter.onTabSelected() is finished, to no avail.

I am using ActionBarSherlock 4.1.0 (and I've substituted in the latest android-support-v4.jar because of a different issue). My Activity extends SherlockFragmentActivity and simply instantiates a ViewPager. I have overridden the FragmentPagerAdapter for the ViewPager's adapter (following the standard example on the ViewPager Android docs).

My phone is running Android 2.3.5.

jfritz42
  • 5,913
  • 5
  • 50
  • 66
  • FWIW, ActionBarSherlock is up to 4.2.0, though I have no idea if that will help. If nobody chimes in with an answer, try creating a sample project that reproduces the error -- if you can do that, I'll take a peek at it. – CommonsWare Dec 21 '12 at 23:27
  • Thanks, I may give that a shot next week. – jfritz42 Dec 21 '12 at 23:42
  • FYI, upgrading to ABS 4.2.0 did not fix the problem. To further describe the problem, each fragment in the ViewPager has a different options menu XML file that it inflates in onCreateOptionsMenu(). The containing SherlockFragmentActivity also has a menu that it inflates in onCreateOptionsMenu(). I tried commenting out the Activity's menu inflation to see if it interferes, but it does not. Same behavior as reported in the question. I'm going to work on making a sample project to demonstrate the bug. – jfritz42 Dec 24 '12 at 18:43
  • I've tried creating a separate sample project and I cannot reproduce the problem. It's kind of maddening! – jfritz42 Dec 28 '12 at 21:41
  • For whatever reason, the app is currently constantly losing a button when I start the app and swipe to the right-most tab. By lose a button, I mean a button from the main Activity is still visible, and to the right of this button is a hole where the Fragment's button should be. The interesting thing is that, once it gets into this state, the ActionBar is frozen this way. I swipe left and right (or click tabs) and the ActionBar constantly has the Activity's button and the hole to the right, even if the current Fragment should add two buttons. – jfritz42 Dec 28 '12 at 21:55
  • :: shrug :: Without a scenario that others can reproduce, it's going to be very difficult to help you. However, if you have the sample working correctly, keep adding hunks of your main app to the sample until either it breaks again (in which case, you've narrowed down the problem) or the whole main app is moved over (in which case, the problem is resolved, and there will be much rejoicing). – CommonsWare Dec 28 '12 at 21:58
  • Another data point: if I call viewPager.postInvalidate() from the TabsAdapter's onPageScrollStateChanged(), it causes the problem to take much longer to appear. I.e. you can swipe between tabs a whole bunch before the ActionBar gets frozen with missing buttons. – jfritz42 Dec 28 '12 at 21:58
  • @CommonsWare thanks for the suggestion. I dread going that route due to the complexity of the app, but I may have to. – jfritz42 Dec 28 '12 at 22:04
  • Well, I discovered that if the main Activity doesn't have a menu and only the fragments have menus, the problem doesn't go away but at least it doesn't get stuck in the "lost a button, ActionBar frozen" problem. You can click another tab and come back and the buttons are there. This is good enough for me for now. – jfritz42 Dec 29 '12 at 00:05
  • I'm facing the exact same issue here. There doesn't seem to be a solution. It is fine, if I click on **any** tab as opposed to swiping. If I swipe, the action bar freezes, and the buttons disappear. – Anirudh Ramanathan Dec 29 '12 at 08:05
  • @Cthulhu, what Android version is on your phone? Which ABS library version? Does your app add/remove tabs dynamically or are they always there? – jfritz42 Dec 31 '12 at 16:38
  • @jfritz42 Happens **only** on Android 4.0.x. Fine on 4.1+ and 2.3.x. The tabs are always there, not being added dynamically. ABS v4.2.0. The swiping of views makes the actionbar button disappear, and after that, no amount of swiping can bring it back. Tested with just 2 tabs. Clicking the tab, instead of swiping makes the actionbar-option come back. – Anirudh Ramanathan Dec 31 '12 at 17:05
  • @Cthulhu Which Android API are you compiling against? I'm using 4.1.2 (API level 16). MinSdkVersion 8, TargetSdkVersion 10. I notice now that if I swipe between the first 3 tabs (the app has 5), everything stays ok. Its the 4th and 5th tab that cause trouble. Tabs 3 and 5 pretty much just contain ListViews. Tab 4 is more complicated with a ListView on top and labels/widgets on the bottom. – jfritz42 Dec 31 '12 at 19:04
  • @jfritz42 I am using `targetSdkVersion: 15` and `minSdkVersion: 8`. – Anirudh Ramanathan Jan 01 '13 at 08:25
  • @jfritz42, I have the same issue on three different devices, running Android 2.3.4, 4.1.1, and 4.2 (all with ABS version 4.1.0). Did you ever work out the cause and/or fix? Thanks. – gcl1 Jan 31 '13 at 16:16
  • @jfrtiz42 A little more info: In my case, tabs always work, but I lose fragment option menu icons the first or second time I swipe away. Also, I tried your suggestion of calling viewPager.postInvalidate() from the TabsAdapter's onPageScrollStateChanged(), but it didn't make any difference. Finally, unlike Cthulhu's case, clicking the tab instead of swiping does not bring the option icon come back; it's gone for good. – gcl1 Jan 31 '13 at 16:24
  • I know this is an old problem but I would like to share my experience. I was facing the same issue where some tabs did not use the search icon. instead of inflating a menu for each tab I simply turn off visibility of icons I do not need and on for icons I need. However, it turned out that when turning off visibility of the search icon it caused, sometimes, the disappearance of other icons, I solved it by re arranging the order of the icons in the bar; so that the search icon is the most right - highest number of `android:orderInCategory` – Zvi Apr 20 '16 at 07:35

2 Answers2

6

Several workarounds for this issue are provided in ViewPager / ActionBar, Menu Items not displaying. The fix discussed in item #8 worked for me.

gcl1
  • 4,070
  • 11
  • 38
  • 55
1

Just follow this link

if (viewPager.getCurrentItem() != position)
  viewPager.setCurrentItem(position);

Defer the call to viewPager.setCurrentItem in onCreate

public void onCreate(...) {
    ...
    view.post(new Runnable() {
        public void run() {
            // guarded viewPager.setCurrentItem
        }
    }
}
Community
  • 1
  • 1
Ayman Mahgoub
  • 4,152
  • 1
  • 30
  • 27