Android fragment OnCreateView called twice

家住魔仙堡 提交于 2019-12-10 13:37:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!