3

I have an Activity that holds some fragments.
This Activity is associated with a view pager, this view pager uses FragmentPagerAdapter, so every page of view pager is treated as a fragment.
Now, suppose I have customize the action bar view in any one of the fragment, and that custom view could be seen in other fragments also.

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

This is because, we are customising the view, using the activity context.
My Question :
Q. Cant we set the Custom View of action bar within a fragment ? So, it would not get reflected to other fragment.

Shanki Bansal
  • 1,681
  • 2
  • 22
  • 32

1 Answers1

2

Short answer: Yes.

You should only allow currently visible fragment to add custom view to the ActionBar. Of course, you can do it right from the Fragment, with whichever context.

Requesting invalidation of the options menu will remove current custom view with the new one, i.e invalidating entire ActionBar. Similar approach you can use from the link above.

Community
  • 1
  • 1
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Not works. Actually the action bar get associated with activity, so any change in view of action bar can be seen in all the fragment. Refreshing view is not the solution. Thanks for your response – Shanki Bansal Oct 06 '14 at 14:06
  • I have an alternative solution,i.e. resetting the display option to ActionBar.DISPLAY_SHOW_HOME and ActionBar.DISPLAY_SHOW_TITLE in all the fragment. But I think, this is not the perfect solution. So, searching for other solution. :) – Shanki Bansal Oct 06 '14 at 14:37