How to handle network calls inside a Fragment

前端 未结 7 1035
攒了一身酷
攒了一身酷 2021-01-31 23:21

I have the following situation:

I have an Activity that hosts a ViewPager, and I have 4 Fragments;

the ViewPager

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 23:53

    Check out the solution from my answer here

    1) Create LifecycleManager Interface The interface will have two methods (onPauseFragment and onResumeFragment) and each ViewPager’s Fragment will implement it
    2) Let each Fragment implement the interface
    3) Implement interface methods in each fragment - start your AsyncTask in onResumeFragment
    4) Call interface methods on ViewPager page change You can set OnPageChangeListener on ViewPager and get callback each time when ViewPager shows another page
    5) Implement OnPageChangeListener to call your custom Lifecycle methods

提交回复
热议问题