简体   繁体   English

Android片段OnCreateView调用了两次

[英]Android fragment OnCreateView called twice

I am using a FragmentPagerAdapter with fragments. 我正在使用FragmentPagerAdapter和片段。 My problem is that when I call setCurrentItem, the OnCreateView method of the fragment that is being loaded is called twice. 我的问题是,当我调用setCurrentItem时,正在加载的片段的OnCreateView方法被调用两次。 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. 我迫不及待地想找到一个只能调用一次OnCreateView的解决方案。

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 Android oncreateview调用了两次

OnCreateView called multiple times / Working with ActionBar and Fragments OnCreateView多次调用/使用ActionBar和Fragments

Fragment onCreateView and onActivityCreated called twice 片段onCreateView和onActivityCreated调用了两次

Most questions I found do not concern FragmentViewPagers, making them mostly unhelpful. 我发现的大多数问题与FragmentViewPagers无关,这使得它们几乎无益。

I would be very grateful for any help! 我会非常感谢任何帮助!

I am using a FragmentPagerAdapter with fragments. 我正在使用FragmentPagerAdapter和片段。 My problem is that when I call setCurrentItem, the OnCreateView method of the fragment that is being loaded is called twice. 我的问题是,当我调用setCurrentItem时,正在加载的片段的OnCreateView方法被调用两次。 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. 我迫不及待地想找到一个只能调用一次OnCreateView的解决方案。

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) . 您遇到的是ViewPager的默认行为,它始终至少缓存其中一个页面( Fragment ),具体取决于setOffscreenPageLimit(int limit)的值。 So onCreateView is called once for each instance returned by getItem 因此,对于getItem返回的每个实例,都会调用onCreateView一次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM