Android片段OnCreateView被两次调用 [英] Android fragment OnCreateView called twice
问题描述
我正在使用带有片段的FragmentPagerAdapter.我的问题是,当我调用setCurrentItem时,正在加载的片段的OnCreateView方法被调用两次.重要的是要注意,该片段不会被重新创建,而只是被恢复.我很想找到一种解决方案,使OnCreateView仅被调用一次.
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:
多次调用OnCreateView/使用ActionBar和Fragments
片段onCreateView和onActivityCreated被调用了两次
我发现的大多数问题与FragmentViewPagers无关,这使它们几乎无济于事.
Most questions I found do not concern FragmentViewPagers, making them mostly unhelpful.
我将非常感谢您的帮助!
I would be very grateful for any help!
推荐答案
我正在使用带有片段的FragmentPagerAdapter.我的问题是 当我调用setCurrentItem时,该片段的OnCreateView方法 被加载的被调用两次.重要的是要注意 该片段未重新创建,只需恢复即可.我是 迫切需要找到一个导致OnCreateView成为 叫过一次.
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.
它不被调用两次.您遇到的是ViewPager
的默认行为,该行为始终缓存至少一个页面(Fragment
),具体取决于setOffscreenPageLimit(int limit)
的值.因此,对于getItem
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
这篇关于Android片段OnCreateView被两次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!