0

I want to make a listener inside my fragment which respond when the fragment become visible. I currently do it with a onPageListener inside the Activity but that doesn't work in my situation. What I need is to execute my AsyncTask in onStart when I enter the fragment. With the onPageListener it tries to do it before.

With a button it works like this :

    ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener()
    {
        @Override
        public void onRefresh()
        {
            // Do work to refresh the list here.
            startNewAsyncTask();
        }
    });

This code comes from a external class which I got from gitHub

A listener which respond to a boolean will already do the thing about not doing it like this : if(true) { code... }

Is that it won't get called when you change to the page you see the viewpager has already started the Fragment so it is inactive when the button is pressed the listener still reacts and that's what I need.

Shishi
  • 601
  • 2
  • 8
  • 27
  • Try `setUserVisibleHint()` like here https://stackoverflow.com/questions/22193785/how-does-fragmentpageradapter-manage-views-in-fragment/22193947#22193947 – super-qua Mar 18 '14 at 14:33
  • Didn't work the problem is a combination of ViewPager/FragmentStatePagerAdapter and AsyncTask the function will get called before/without onAttach and it will refer to the old Activity. The listener is something which I'm certain of that it will work I basically only need to execute it when the fragment becomes visible instead of after the button is pressed – Shishi Mar 18 '14 at 14:42

1 Answers1

0

try calling your function from your fragment class' onResume() method

upenpat
  • 685
  • 3
  • 12
  • The function is called from the onPageChangeListener inside the MainActivity's FragmentStatePager when I move the onPageChangeListener to my fragment it won't respond. So what I want to do is let the onPageChangeListener execute a function inside the fragment which activates a listener – Shishi Mar 18 '14 at 14:52
  • **my fragment it won't respond**. have you written the asyntask properly? perhaps you want to shift more work to doInBackground there. – upenpat Mar 20 '14 at 12:26