为什么每当我在应用程序的viewPager中进行分页时,我的片段onCreate都会被广泛调用? [英] Why is my fragment onCreate being called extensively whenever I page through my applications viewPager?

查看:118
本文介绍了为什么每当我在应用程序的viewPager中进行分页时,我的片段onCreate都会被广泛调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解这个碎片化的生命周期业务.

I'm not quite understanding this fragment lifecycle business.

我在我的应用程序的查看详细信息"部分有一个非常标准的3页水平滑块视图分页器布局.我在三个页面的中间页面启动我的应用程序.我的FragmentActivity将页面设置为页面1.

I have a pretty standard 3 page horizontal slider view Pager layout for a "view details" section of my app. I start my app on the middle page of the three. My FragmentActivity sets the page to page 1.

mPager.setCurrentItem(1); //default to center page, current data view fragment

我一直在使用FragmentStatePagerAdapter,因为使用FragmentPagerAdapter有时会使我的应用程序崩溃,例如,从挂起状态返回时,这是目前解决该问题的最快方法.看起来可行,但我怀疑可能是我的问题所在是国家方面.

I've been using the FragmentStatePagerAdapter because using the FragmentPagerAdapter crashed my app at times, when coming back from a suspended state for example, and this was the quickest way to work around that for now. Seems to work but I suspect the State aspect is what might be the cause of my problem here, maybe.

因此,起初我以为我应该让每个片段都从Rest服务中获取数据,然后将其显示在列表中,但是我不太确定.

So at first I thought that I would have each fragment do the work of getting data from a rest service and then showing it in a list, but I'm not so sure any more.

我尝试运行一个独特的异步任务以在onCreateView事件的每个片段中获取数据. 然后,在阅读了更多有关片段生命周期的信息后,我切换到onCreate,注意到onCreateView被大量调用,这又使贪婪的应用程序经常通过网络请求数据.

I tried running a unique async task to fetch data in each of the fragments onCreateView events. Then after reading more on the fragment lifecycle I switched to onCreate, having noticed that the onCreateView was being called quite heavily, which in turn made for a greedy app that too often requested data over the wire.

切换到onCreate并没有做任何更改.对于两个相邻的片段,OnCreate的调用与onCreateView一样多.

Switching to onCreate hasn't changed anything. OnCreate is still geting called just as much as onCreateView for the 2 adjacent fragments.

奇怪的是,我设置为片段活动"中第一个显示的片段只获得了一次被称为的onCreate.

The odd thing is that the fragment that I set to be the first one to display in Fragment Activity only gets the onCreate called the one time.

这里感觉不对.

现在,我正在考虑让父片段活动声明并调用所有异步任务以获取需要在片段中显示的数据. 在父片段活动所拥有的对象中设置异步调用的结果,然后让片段使用父片段所包含的对象来创建列表视图等.

Right now I'm thinking that I should have the parent fragment activity declare and call all the async tasks to fetch the data that I need to display in the fragments. Set the results of the async calls in an object owned by the parent fragment activity and then have the fragments use the object contained by the parent to create the list view etc.

但是,如果在调用onCreateView的每个片段之前父活动开始的异步任务没有完成并且父包含的对象还没有准备好.....

But what if the async tasks started by the parent activity don't finish before each fragments onCreateView is called and the object contained by the parent isn't ready yet.....

签名,困惑和沮丧

推荐答案

ViewPager非常热衷于关闭当前不使用的东西,而这正是这里发生的事情. ViewPager的默认行为是保留"正在查看的页面两侧的一页,并销毁其余页面.因此,在您的3页视图中,选择第1页时第3页会被破坏,然后当重新选择第2页时会重新创建第3页.您已经注意到,第2页仅被调用一次onCreate(..),因为它总是与当前选定的页面相邻或相邻.

ViewPager is quite zealous in shutting down things it isn't currently using, and this is exactly what is happening here. The default behaviour is for ViewPager to "keep around" one page either side of the page being viewed, and destroy the rest. Hence in your 3-page view, page 3 gets destroyed when selecting page 1, then when page 2 is reselected page 3 is recreated. As you've noticed, page 2 only has onCreate(..) called once because it is always adjacent to, or is, the currently selected page.

要解决此问题,只需设置ViewPager.setOffscreenPageLimit(2).然后,ViewPager将保留所有片段.显然,对于大量的Fragments来说,这不是一个好主意,但是对于您的情况,应该没问题.

To solve this, simply set ViewPager.setOffscreenPageLimit(2). Then the ViewPager will keep all your Fragments. Obviously this isn't a good idea for a large number of Fragments, but for your case it should be fine.

这篇关于为什么每当我在应用程序的viewPager中进行分页时,我的片段onCreate都会被广泛调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆