[英]Understanding fragment activity lifecycle
I have created an android app that uses a ViewPager to swipe through three fragments (each of the three fragments contains a gridview). 我创建了一个Android应用程序,它使用ViewPager来浏览三个片段(三个片段中的每一个都包含一个gridview)。
I began learning about fragments recently and assumed that after my gridviews were created in their fragments, that each fragment would be in memory and never have to load again. 我最近开始学习片段,并假设在我的网格视图在其片段中创建后,每个片段都将在内存中,而不必再次加载。
Recently I noticed that when I swiped from my left fragment, to the middle fragment, then to the right fragment, that the left-most fragment would have to have its gridview filled again with my adapter. 最近我注意到,当我从左侧片段滑动到中间片段,然后到右侧片段时,最左边的片段必须再次使用我的适配器填充其gridview。
I was just wondering why this happens when I navigate from the left-most fragment to the right-most fragment, but not when I navigate between side-by-side fragments. 我只是想知道为什么当我从最左边的片段导航到最右边的片段时会发生这种情况,而不是当我在并排片段之间导航时。 Does it only keep the most recent fragment in memory and kill the other fragment?
它是否只将最新的片段保留在内存中并杀死其他片段? Or is there some other reason why an app wouldn't keep all three fragments in memory?
或者是否有其他原因导致应用程序无法将所有三个片段保留在内存中? My app gets a little laggy and slow when I quickly navigate between fragments, so it would be nice to only have to draw each gridview one time if possible.
当我在片段之间快速导航时,我的应用程序会有点滞后和缓慢,所以如果可能的话,只需要一次绘制每个gridview就好了。
I fill my arraylist (used to fill adapter) in onCreate(), and then fill my gridview with the adapter in onActivityCreated(Bundle savedInstanceState) 我在onCreate()中填充我的arraylist(用于填充适配器),然后用onActivityCreated中的适配器填充我的gridview(Bundle savedInstanceState)
The ViewPager
keeps a certain amount of off-screen tabs ( Fragment
s, of course) in memory. ViewPager
在ViewPager
保留了一定数量的屏幕外标签(当然是Fragment
)。 The default, I believe for all devices, is 1. Thus, when you scroll to the far right, only the one to its left will be kept in memory. 我相信所有设备的默认值为1.因此,当您滚动到最右侧时,只有左侧的那个将保留在内存中。
If you want your ViewPager
to retain all tabs in memory (careful--this can be hard on the device or other running apps), you can set how many off-screen tabs to keep in memory. 如果您希望
ViewPager
保留内存中的所有选项卡(小心 - 这可能在设备或其他正在运行的应用程序上很难),您可以设置要在内存中保留多少屏幕外选项卡。 To do this, use the setOffscreenPageLimit
method of ViewPager
on your ViewPager
object. 为此,
ViewPager
在ViewPager
对象上使用setOffscreenPageLimit
方法。
Example: 例:
ViewPager pager = (ViewPager) findViewById(R.id.pager); // Your pager's ID
pager.setOffscreenPageLimit(2); // Will retain 2 off-screen tabs + your selected tab
Hope this helps! 希望这可以帮助!
It's the nature of how a FragmentPagerAdapter
and FragmentPagerStateAdapter
work. 这是
FragmentPagerAdapter
和FragmentPagerStateAdapter
如何工作的本质。 From Google's site: 来自Google的网站:
The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible.
Meaning, your GridView
is refilling because your ViewPager
killed the view and has to rebuild it. 这意味着,您的
GridView
正在重新填充,因为您的ViewPager
了视图并且必须重建它。
EDIT: If you need to keep all three fragment Views
in memory to speed things up, then you would have to create your own PagerAdapter
that stores the views in instantiateItem(ViewGroup, int)
inside an Collection
of some sort. 编辑:如果你需要将所有三个片段
Views
保留在内存中以加快速度,那么你必须创建自己的PagerAdapter
,它将视图存储在某种Collection
中的instantiateItem(ViewGroup, int)
。
Alternatively, if you are using Google's example verbatim, then you probably are doing something like this: 或者,如果您逐字使用Google的示例,那么您可能正在执行以下操作:
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
In which case, you're rebuilding a new fragment each time the ViewPager requests one. 在这种情况下,每次ViewPager请求时,您都在重建一个新片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.