Search code examples
androidandroid-fragmentsandroid-tabs

manage life cycle of fragments that use in SlidingTabLayout


in my app i use SlidingTabLayout to make tabs ,my app has three tabs for each tab attach with fragment hence i have three classes extend from Fragment when Run App show me onCreateView and OnCreate of first and second fragment created together i want to when click on second tab onCreateView and OnCreate of second tab created because i load things from webservice when click second tab like facebook app that happen loading things when click on tab


Solution

  • Looks like you're using SlidingTabLayout combined with ViewPager. ViewPager by default manages 3 items (curent one on one on the "left"/"right" side) one of which is displayed and 2 others are in idle state. You can't decrease number of such items so the only way for you is to move your web service call from onCreate/onViewCreated to some other place. For example you may call setHasOptionsMenu(true) in your second fragment and implement your web service call in onOptionsMenuCreated callback of your second fragment.