user2121 February 2016

life cycle callbacks of fragments are not called accordingly

I have a main activity with view pager and three action tabs "fragment". and i implemented the life cycle callbacks for each of the main activity and the three fragments. and in each of the life cycle call back i placed a log statements indicates which life cycle callback being called to know how the action bars with view pager behaves. at run time i perceived a strange behaviour which i cant understand or attribute it to any reason.

first behaviour

when the App fisrt starts I receive:

02-08 15:16:14.771 32243-32243/ W/MainActivity: onCreate()
02-08 15:16:14.901 32243-32243/ W/MainActivity: onStart()
02-08 15:16:14.901 32243-32243/ W/MainActivity: onResume()
02-08 15:16:14.941 32243-32243/ W/Frag_1: onAttach()
02-08 15:16:14.941 32243-32243/ W/Frag_1: onCreate()
02-08 15:16:14.941 32243-32243/ W/Frag_1: onCreateView()
02-08 15:16:14.941 32243-32243/ W/Frag_1: onStart()
02-08 15:16:14.941 32243-32243/ W/Frag_1: onResume()
02-08 15:16:14.941 32243-32243/ W/Frag_2: onAttach()
02-08 15:16:14.941 32243-32243/ W/Frag_2: onCreate()
02-08 15:16:14.941 32243-32243/ W/Frag_2: onCreateView()
02-08 15:16:14.941 32243-32243/ W/Frag_2: onStart()
02-08 15:16:14.941 32243-32243/ W/Frag_2: onResume()

and i cant find any reason why only the life cycle callbacks of the mainactivty, frag_1 and frag_2 were called BUT NOT frag_3? any explaination?

2nd behaviour

occured when i touched tab3 "frag_3", i received:

02-08 15:16:36.031 32243-32243/ D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
02-08 15:16:36.121 32243-32243/        


Daniel Zolnai February 2016

In the ViewPager, fragments are being created ahead of time.

That means, that your Frag_2 is not visible yet (because Frag_1 covers the entire screen), but it still creates the view beforehand, so scrolling to Frag_2 will be smooth.

By default, the ViewPager preloads one fragment on each side. So if you would set the start position of the ViewPager to Frag_2, it would load Frag_1 and Frag_2 as well, because they are the neighbors.

You'll notice that Frag_3 will be created when you swipe to Frag_2

You can increase the number of preloaded fragments by calling ViewPager.setOffscreenPageLimit(int limit).

Note that the min. number is 1, so this can not be disabled.

Estiven Restrepo February 2016

The life cycle callbacks are ok..

Check the setOffscreenPageLimit method, you can use it to Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. By default it set 1, thats the reason because your have that behaviour..

Post Status

Asked in February 2016
Viewed 3,470 times
Voted 5
Answered 2 times


Leave an answer