In my application, I have a ViewPager which holds many swipeable Tabs with Fragments inside. I use the setUserVisibleHint
method to detect when a Fragment
Well, its workaround more, and maybe its better to setup OnPageChangeListener
, but it works for me. The task is to determine whether the current fragment is visible at creation. You can use the menu to do this. In your fragment:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true); // set current fragment has its own options menu
}
and this
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
if(isMenuVisible()) { // menu should be visible if current fragment is visible right now
setUserVisibleHint(true); // manually set value to true
}
...
return view;
Maybe someone will find it usefull. This works for me.