44

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?

Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • 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. – Karakuri Sep 04 '14 at 14:43

6 Answers6

66

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
    } 
});
rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Paul Burke
  • 25,496
  • 9
  • 66
  • 62
4

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
   }
Rahul
  • 3,293
  • 2
  • 31
  • 43
3

You have to call manually mViewPagerListener.onPageSelected(0); in onCreateView in your root activity/fragment.

Yuraj
  • 3,185
  • 1
  • 23
  • 42
0

I dont understand what you need exactly but you can detect the page of the current fragment by calling ViewPager.getCurrentItem that return integer.

Hope it helps.

Good luck.

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
-1

Well, its workaround more, and maybe its better to setup OnPageChangeListener, but it works for me. The task is to determine whether the current fragment is visible at creation. You can use the menu to do this. In your fragment:

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true); // set current fragment has its own options menu
    } 

and this

 @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     ...

            if(isMenuVisible()) { // menu should be visible if current fragment is visible right now
                setUserVisibleHint(true); // manually set value to true
            }
     ...
    return view;

Maybe someone will find it usefull. This works for me.

kara4k
  • 407
  • 5
  • 11
-6

I check your problem,but I find out the onCreateView method is called before the setUserVisibleHint method

enter code here

because the setUserVisibleHint method is called before the onCreateView method

chinaanihchen
  • 396
  • 2
  • 5