Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fragment OnCreateView called twice

I am using a FragmentPagerAdapter with fragments. My problem is that when I call setCurrentItem, the OnCreateView method of the fragment that is being loaded is called twice. It is important to note however that the fragment is not recreated, it is simply resumed. I am desperate to find a solution that causes OnCreateView to only be called once.

I have taken a look at the following questions and some more, but none of them contain answers for my case:

Android oncreateview called twice

OnCreateView called multiple times / Working with ActionBar and Fragments

Fragment onCreateView and onActivityCreated called twice

Most questions I found do not concern FragmentViewPagers, making them mostly unhelpful.

I would be very grateful for any help!

like image 878
Userrrrrrrrr Avatar asked Jul 11 '15 08:07

Userrrrrrrrr


Video Answer


1 Answers

I am using a FragmentPagerAdapter with fragments. My problem is that when I call setCurrentItem, the OnCreateView method of the fragment that is being loaded is called twice. It is important to note however that the fragment is not recreated, it is simply resumed. I am desperate to find a solution that causes OnCreateView to only be called once.

It is not called twice. What you are experiencing is the default behavior of the ViewPager, which caches always at least one of its page (Fragment), depending of the value of setOffscreenPageLimit(int limit). So onCreateView is called once for each instance returned by getItem

like image 92
Blackbelt Avatar answered Sep 20 '22 07:09

Blackbelt