In action bar set custom view in fragment

I have an Activity that contains some fragments.
This activity is associated with a view pager, this view pager uses a FragmentPagerAdapter, so each view pager page is treated as a fragment.
Now, suppose I set up an action bar view in any of the snippets, and that custom view could be seen in other snippets as well.

getActivity().getSupportActionBar().setCustomView(R.layout.custom_view_home);
        getActivity().getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);   

      

This is because we are setting up the view using the activity context.
My question is:
Q. Can't we set a custom view of the action bar inside a fragment? This way it would not be reflected in another fragment.

+3


source to share


1 answer


Short answer: Yes .

You should only allow the currently visible fragment to add a custom view to ActionBar

. Of course, you can do this directly from Fragment with any context.



Requesting the options menu to invalidate will delete the current custom view with the new, i.e. invalidate the entire ActionBar. A similar approach can be used from the link above.

+2


source







All Articles