Android片段OnCreateView调用了两次

我正在使用FragmentPagerAdapter和片段.我的问题是,当我调用setCurrentItem时,正在加载的片段的OnCreateView方法被调用两次.重要的是要注意,片段不会重新创建,只需恢复即可.我迫不及待地想找到一个只能调用一次OnCreateView的解决方案.

我已经看过以下问题以及更多问题,但它们都没有包含我的案例的答案:

Android oncreateview called twice

OnCreateView called multiple times / Working with ActionBar and Fragments

Fragment onCreateView and onActivityCreated called twice

我发现的大多数问题与FragmentViewPagers无关,这使得它们几乎无益.

我会非常感谢任何帮助!

最佳答案

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的默认行为,它始终至少缓存其中一个页面(片段),具体取决于setOffscreenPageLimit(int limit)的值.因此,对于getItem返回的每个实例,都会调用onCreateView一次

转载注明原文:Android片段OnCreateView调用了两次 - 代码日志