ActionBarSherlock - Overflow menu does not appear at first launch on 2.x devices when using setHasOptionsMenu true on fragments

StackOverflow https://stackoverflow.com//questions/12671785

Question

If hasSetOptionsMenu is true for fragments, the overflow menu is not shown each time on first launch on OS 2.x. Rotating device solves the issue. Also, issue is not reproducible on 3.x or 4.x.

I am using the 4.1 version of the ActionBarSherlock, and the FragmentActivity holding the fragments has a forceoverflow theme.

Tried to set hasSetOptionsMenu to false and this seems to solve the issue, but I have to add a different menu item for each fragment, so this would not be an option for me.

Debugging the app, it seems that the menu is invalidated for each hasSetOptionsMenu(true) in the app, and the activity's and fragment's onCreateOptionsMenu is recreated. Also, an interesting fact it would be that during debug the app startup is delayed as it stops at different breakpoints and this causes the overflow menu to appear. This convinces me it could be this issue reported in Android SDK but neither of the workarounds mentioned in comment #8 worked for me.

Another info: pressing the hardware options button, the overflow menu is presented, so it seems that the problem is that the overflow icon is not displayed.

Also I discovered this problem was also a reported bug in ABS but it seems closed as fixed and I have the last version of the library.

Has anyone encountered this before? And do you know a workaround/fix for this?

Was it helpful?

Solution

Solve it! Added "getSherlock().dispatchInvalidateOptionsMenu();" in onResume() for the FragmentActivity to force the menu invalidation. This seems to have solved the issue. I know this is a hack but did not managed to find another solution. Tried adding the menu invalidation to the fragments (onCreateView or onActivityCreated) but this did not fixed it. So if you encounter this issue, this is a workaround for it:

    @Override
    protected void onResume() {
        super.onResume();

        .....
        getSherlock().dispatchInvalidateOptionsMenu();
    }

Thanks Marco for the tip!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top