Android: How to recreate Action bar when fragment changed

后端 未结 4 1808
借酒劲吻你
借酒劲吻你 2021-01-31 09:54

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

4条回答
  •  无人共我
    2021-01-31 10:41

    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

提交回复
热议问题