ViewPager call setUserVisibleHint after first Fragment is loaded

23,403

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.

Share:
23,403

Related videos on Youtube

Cilenco
Author by

Cilenco

Student at the university of Münster (Germany). I concentrate on Android development and efficient algorithms and data structures.

Updated on July 09, 2022

Comments

  • Cilenco
    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 a Fragment 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 first Fragment because the setUserVisibleHint method is called before the onCreateView 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
      Karakuri almost 10 years
      When 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
    savepopulation over 9 years
    if you want to recreate actionbar menu when fragment changed you should implement it onpagechangelistener like @paul says.
  • ilovett
    ilovett over 9 years
    Thank you. Sometimes onCreateView and setUserVisibleHint called out of other... This method ensures after both have been called.
  • Arjun Mathew Dan
    Arjun Mathew Dan almost 9 years
    Paul : How can I get this working the first time for the first page?
  • CrandellWS
    CrandellWS about 8 years
    use addOnPageChangeListener instead of setOnPageChangeListener stackoverflow.com/a/30867886/1815624
  • Rahul
    Rahul almost 6 years
    why downvote?? if anyone find any issue with it please let me know
  • Jose_GD
    Jose_GD over 5 years
    maybe because using global variables?
  • Rahul
    Rahul over 5 years
    @Jose_GD what is the problem with global variables??
  • AngryDuck
    AngryDuck over 5 years
    Agreed this doesn't work at all for the first page loaded
  • Coli
    Coli over 4 years