ViewPager call setUserVisibleHint after first Fragment is loaded
Solution 1
You can't (and shouldn't) rely on setUserVisibleHint
for this. Instead, you should be using a ViewPager.OnPageChangeListener to get callbacks for when a page becomes visible. E.g.
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// do your work
}
});
Note: You can use ViewPager.SimpleOnPageChangeListener
if you don't need to listen for all callbacks.
Update
setOnPageChangeListener
method is now deprecated, use addOnPageChangeListener
instead
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// do your work
}
});
Solution 2
BELOW WORKED FOR ME
Please create a global view like this
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//inflate view layout
view =inflater.inflate(R.layout.your_fragment, container, false);
// return view
return view;
}
and use this
@Override
public void setUserVisibleHint(boolean isUserVisible)
{
super.setUserVisibleHint(isUserVisible);
// when fragment visible to user and view is not null then enter here.
if (isUserVisible && view != null)
{
onResume();
}
}
and In OnResume method
@Override
public void onResume() {
super.onResume();
if (!getUserVisibleHint()) {
return;
}
//do your stuff here
}
Solution 3
You have to call manually mViewPagerListener.onPageSelected(0);
in onCreateView
in your root activity/fragment.
Related videos on Youtube
![Cilenco](https://i.stack.imgur.com/WY0Zw.png?s=256&g=1)
Cilenco
Student at the university of Münster (Germany). I concentrate on Android development and efficient algorithms and data structures.
Updated on July 09, 2022Comments
-
Cilenco almost 2 years
In my application, I have a ViewPager which holds many swipeable Tabs with Fragments inside. I use the
setUserVisibleHint
method to detect when aFragment
comes to the screen. This works great when the user swipes between tabs but it does not work on the first load. To run the code in the method I have to swipe to left and then back to the firstFragment
because thesetUserVisibleHint
method is called before theonCreateView
method.Do you have any ideas how I can run this code after the first Fragment is visible? Is there a method in the
ViewPager
or something else?-
Karakuri almost 10 yearsWhen I tried to do something similar (detect which of the fragments in the viewpager was the current visible page), I had this exact same problem. Every solution I tried was a hack and it was never quite right.
-
-
savepopulation over 9 yearsif you want to recreate actionbar menu when fragment changed you should implement it onpagechangelistener like @paul says.
-
ilovett over 9 yearsThank you. Sometimes onCreateView and setUserVisibleHint called out of other... This method ensures after both have been called.
-
Arjun Mathew Dan almost 9 yearsPaul : How can I get this working the first time for the first page?
-
CrandellWS about 8 years
-
Rahul almost 6 yearswhy downvote?? if anyone find any issue with it please let me know
-
Jose_GD over 5 yearsmaybe because using global variables?
-
Rahul over 5 years@Jose_GD what is the problem with global variables??
-
AngryDuck over 5 yearsAgreed this doesn't work at all for the first page loaded
-
Coli over 4 yearsSee this comment: stackoverflow.com/questions/9779397/…