How to know when fragment actually visible in viewpager
Of course. Assuming that viewPager
is your instance of the ViewPager
, use: viewPager.getCurrentItem()
.
Within your Fragment
you can check if its instance is visible to the user like so:
@Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(visible);
if (visible) {
Log.i("Tag", "Reload fragment");
}
}
Always make sure that you search for answers throughly before asking your question. For instance, the first place you should check would be: https://developer.android.com/reference/android/support/v4/view/ViewPager.html
as per @Matt's answer setUserVisibleHint
is deprecated
so here is alternative way for this.
@Override
public void setMenuVisibility(boolean isvisible) {
super.setMenuVisibility(isvisible);
if (isvisible){
Log.d("Viewpager", "fragment is visible ");
}else {
Log.d("Viewpager", "fragment is not visible ");
}
}