2

I am using this method but it is not working for first fragment but on swiping from second to first fragment it working fine. please help me in this. thanks

 @Override
     public void setUserVisibleHint(boolean isVisibleToUser) 
   {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){   //do Something 
       }
     }
Rahul
  • 3,293
  • 2
  • 31
  • 43

3 Answers3

1

This is how it works

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 inside onResume put this code

@Override
         public void onResume() { 
         super.onResume();   
            if (!getUserVisibleHint()) {
            return;
          }
      //do your stuff here
   }
Rahul
  • 3,293
  • 2
  • 31
  • 43
0

You really shouldn't rely on the order in which setUserVisibleHint is called when using the support version. From the docs:

Note: This method may be called outside of the fragment lifecycle. and thus has no ordering guarantees with regard to fragment lifecycle method calls.

A similar question has some approaches on this.

Community
  • 1
  • 1
Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40
  • then how can i load data when tab is visible ?? i guess this is the only method which can help with this situation...YouTube is also using this kind of approach that when user tap then it loads the clicked page – Rahul Mar 14 '17 at 07:11
  • Are you using a `ViewPager` ? – Veneet Reddy Mar 14 '17 at 08:39
  • You can use `OnPageChangeListener` to know when a page is selected: https://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html#onPageSelected(int) – Veneet Reddy Mar 14 '17 at 15:25
  • but i need to hit a webapi on that fragment so that I can display data on that fragment and this method is used in activity and transfer of data from one page to other is more with this approach – Rahul Mar 15 '17 at 04:14
0

I faced this issue when I was loading data on viewPager Fragments as and when they were visible.To load data only when a particular fragment was visible , I relied on setUserVisibleHint(isVisibleToUser:Boolean) without realising at what point in fragment lifecycle it was being called.

As a result, I was all clueless about a day and two, why the hell where all my variables(present in onCreateView()) were null.Only After going through some stack Answers I realised the mistake I was committing.

setUserVisibleHint() was called even before onCreateView() was called.

So the work around is this . See the highest voted answer here.The guy managed it with Booleans. Hope it helps all the future visitors and save their time.

iCantC
  • 2,852
  • 1
  • 19
  • 34