Android: How to recreate Action bar when fragment changed
Asked Answered
C

4

24

I have an activity showing a few fragments. Activity view contains only ViewPager initialized with custom FragmentPagerAdapter. This adapter provide navigation among 3 fragments.

All seems to work fine except Action bar.

I override onCreateOptionsMenu() method in my fragments to create individual Action bar for any fragment:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();

        //fragment specific menu creation
    }

When I do swipe, new fragment appears, but Action bar stay the same for a few seconds. Probably after a few seconds this method are called and Action bar get changed.

This looks pretty bad when action bar are changed in a while after swipe is finished. How can I recreate action bar immediately, before swipe begin?

Coker answered 23/1, 2013 at 12:27 Comment(0)
B
26

You can ask android to re-create the actionbar before it automatically does by calling invalidateOptionsMenu();

Do this somewhere close to the point where you change fragments to try and decrease the 'lag' between the fragment and actionbar changing.

Edit

a complete solution may look like this:

class activity extends Activity{

private void switchFragment(){

...do fragment changing stuff

activity.invalidateOptionsMenu();

}

}

class someFragment extends Fragment{

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();


    //fragment specific menu creation
}

}

whichever fragment is open during the

activity.invalidateOptionsMenu();

will then call its

 onCreateOptionsMenu

and you can do fragment specific menu creation in there

Batholomew answered 23/1, 2013 at 13:14 Comment(3)
Problem is I couldn't find any obvious way to ask android to re-create actionbar.Coker
@Mik i've updated my answer to help but i don't really understand you fully... invalidateOptionsMenu() should "ask android to re-create actionbar"Batholomew
Thanks, for the answer, but 1. When you call .invalidateOptionsMenu(); system doesn't really re-create action bar. It only cause calling onCreateOptionsMenu() in your fragment allowing you to change Menu(which is part of actionbar). If you called setHasOptionsMenu(true) in your fragment, then you don't have to even call invalidateOptionsMenu(), because onCreateOptionsMenu() is called automatically. 2. I couldn't found a way to call onCreateOptionsMenu() before swipe made.Coker
M
4

It can be done by implementing onCreateOptionsMenu and calling setHasOptionsMenu(true) in the onAttach callback in each fragment and change the actions accordingly.

To show the fragment without any actions setHasOptionsMenu(false) in it

Madigan answered 18/5, 2013 at 8:16 Comment(1)
Thank you for mentioning onAttach and setHasOptionsMenu!Inspectorate
V
2

If you are using ActionBar tabs, You would like to see this answer:

How can I change Action Bar actions dynamically?

Verulamium answered 31/8, 2014 at 15:43 Comment(0)
C
1

Finally I couldn't find any sound way to achieve this.

The best would be to make actionbar a part of fragment, not activity, but it's impossible.

I end up with clearing actionbar menu and title when swipe begins(you can set special listener in PageView) and setting menu and title again when swipe complete and new fragment are shown.

In gives some time lag and actionbar looks strangely empty during swipe, but it's best you can do.
Android API is c...

Coker answered 27/1, 2013 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.