May be very late. This is working for me. I slightly updated the code from @Gobar and @kris Solutions. We have to update the code in our PagerAdapter
.
setPrimaryItem
is called every time when a tab is visible and returns its position. If the position are same means we are unmoved. If position changed and current position is not our clicked tab set as -1.
private int mCurrentPosition = -1;
@Override
public void setPrimaryItem(@NotNull ViewGroup container, int position, @NotNull Object object) {
// This is what calls setMenuVisibility() on the fragments
super.setPrimaryItem(container, position, object);
if (position == mCurrentPosition) {
return;
}
if (object instanceof YourFragment) {
YourFragment fragment = (YourFragment) object;
if (fragment.isResumed()) {
mCurrentPosition = position;
fragment.doYourWork();//Update your function
}
} else {
mCurrentPosition = -1;
}
}