Android 片段 OnCreateView 调用了两次

Android fragment OnCreateView called twice

我正在使用带有片段的 FragmentPagerAdapter。我的问题是,当我调用 setCurrentItem 时,正在加载的片段的 OnCreateView 方法被调用了两次。然而,重要的是要注意片段不会被重新创建,它只是被恢复。我很想找到一个解决方案,使 OnCreateView 只被调用一次。

我查看了以下问题以及更多问题,但其中 none 包含针对我的案例的答案:

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 的默认行为,它始终至少缓存其页面之一 (Fragment),具体取决于 setOffscreenPageLimit(int limit) 的值。因此 onCreateViewgetItem

返回的每个实例调用一次