Search code examples
androidandroid-fragmentsandroid-actionbarandroid-viewpagerandroid-actionbar-compat

Why my Android ActionBar doesn't update when it is explictily changed


I've seen questions on SO that related to these many times. So I've decided to post the reason why it happens and also the solution so that many SO users will get the benefit of the answer.

For sometime now I've seen a reoccurring issue where I would change the ActionBar title and it would get ellipsized shorter than it should. Another symptom of this is having your title wrap when it shouldn't or just stay completely blank. When you change the the action bar buttons it wouldn't update correctly and I would be left with either no action bar buttons even though they have definitely been added or double up on existing action bar buttons.

It turns' out this is a relatively straight forward issue. It mainly happens when you use a ViewPager with the getChildFragmentManager() to manage your fragments.


Solution

  • The fragment that hosts the ViewPager and the child Fragments that are created by your adapter must also call setHasOptionsMenu(true); in their onCreate() method's, so that all of the option menu events get passed down to the child fragments and on to their children and so on.

    Simple really, when you think about it. But its just one of those things.

    Hope this helps and I welcome your comments, Simon