Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my fragment onCreate being called extensively whenever I page through my applications viewPager?

I'm not quite understanding this fragment lifecycle business.

I have a pretty standard 3 page horizontal slider view Pager layout for a "view details" section of my app. I start my app on the middle page of the three. My FragmentActivity sets the page to page 1.

mPager.setCurrentItem(1); //default to center page, current data view fragment

I've been using the FragmentStatePagerAdapter because using the FragmentPagerAdapter crashed my app at times, when coming back from a suspended state for example, and this was the quickest way to work around that for now. Seems to work but I suspect the State aspect is what might be the cause of my problem here, maybe.

So at first I thought that I would have each fragment do the work of getting data from a rest service and then showing it in a list, but I'm not so sure any more.

I tried running a unique async task to fetch data in each of the fragments onCreateView events. Then after reading more on the fragment lifecycle I switched to onCreate, having noticed that the onCreateView was being called quite heavily, which in turn made for a greedy app that too often requested data over the wire.

Switching to onCreate hasn't changed anything. OnCreate is still geting called just as much as onCreateView for the 2 adjacent fragments.

The odd thing is that the fragment that I set to be the first one to display in Fragment Activity only gets the onCreate called the one time.

Something doesn't feel right here.

Right now I'm thinking that I should have the parent fragment activity declare and call all the async tasks to fetch the data that I need to display in the fragments. Set the results of the async calls in an object owned by the parent fragment activity and then have the fragments use the object contained by the parent to create the list view etc.

But what if the async tasks started by the parent activity don't finish before each fragments onCreateView is called and the object contained by the parent isn't ready yet.....

signed, confused and frustrated

like image 364
topwik Avatar asked Aug 07 '12 19:08

topwik


People also ask

Can I use onCreate in fragment?

Fragment has many methods which can be overridden to plug into the lifecycle (similar to an Activity): onAttach() is called when a fragment is connected to an activity. onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view.

What is difference between onCreate and onCreateView in fragment?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.


1 Answers

ViewPager is quite zealous in shutting down things it isn't currently using, and this is exactly what is happening here. The default behaviour is for ViewPager to "keep around" one page either side of the page being viewed, and destroy the rest. Hence in your 3-page view, page 3 gets destroyed when selecting page 1, then when page 2 is reselected page 3 is recreated. As you've noticed, page 2 only has onCreate(..) called once because it is always adjacent to, or is, the currently selected page.

To solve this, simply set ViewPager.setOffscreenPageLimit(2). Then the ViewPager will keep all your Fragments. Obviously this isn't a good idea for a large number of Fragments, but for your case it should be fine.

like image 186
Alex Curran Avatar answered Sep 17 '22 13:09

Alex Curran