I have 4 fragments in my viewpager I want to send a call for data every time when my fragment is visible to user. I am using uservisibilityhint() function but it is called only first time in view page and then again it is never called whenever that fragment is visible . is there any way to call it manually every time when the fragment is visible so that I can now that my fragment is visible or not to the user . how can i do this thing
2 Answers
Use
@Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(visible);
if (visible && isResumed()) {
// Your code
}
}
-
i already wrote in my question that this function is called only for first time then it is never called Commented Oct 15, 2018 at 7:45
Use a Listener
on your ViewPager
in the Activity
like below:
viewPager.setOnPageChangeListener(new OnPageChangeListener...
and here in the listeners callback detect the current View using this method:
ViewPager.getCurrentItem()
and then wire an interface between your activity and four fragments so you can notify which View should try loading fresh data.