且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android片段OnCreateView被两次调用

更新时间:2023-12-02 23:14:04

我正在使用带有片段的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