问题
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!
回答1:
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
来源:https://stackoverflow.com/questions/31355150/android-fragment-oncreateview-called-twice