保存状态和FragmentStatePagerAdapter还原状态 [英] Save State and Restore State in FragmentStatePagerAdapter

查看:1080
本文介绍了保存状态和FragmentStatePagerAdapter还原状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一个使用 ViewPager FragmentStatePageAdapter 我的屏幕上我有大量的图片,并享有5页。目前,我有 mViewPager.setOffscreenPageLimit(1); 所以只有当前,previous和明年将在内存和其他2将被销毁。但对于破坏片段我想利用 saveState和() restoreState()适配器保持其状态,所以,当我回来的屏幕将反正去 onCreateView()该片段是什么将保持状态也。

I a using ViewPager with FragmentStatePageAdapter on my screen I have 5 pages which has lots of images and views. Currently I have mViewPager.setOffscreenPageLimit(1); so only current, previous and next will be in memory and other 2 will be destroyed. But for that destroyed fragments I want to make use of saveState() and restoreState() of the adapter to maintain its state so when I come back to that screen it will anyways go to onCreateView() of that fragment what will maintain state also.

mViewPager.setOffscreenPageLimit(4); 是不是一个好的选择,因为它有内存问题。

Having mViewPager.setOffscreenPageLimit(4); is not a good option as it has memory issues.

我搜索了很多,但我没有得到这使得利用这一功能2,保持和恢复状态的任何样本。

I searched a lot but I didn't get any sample which make use of this 2 functions to maintain and restore state.

有人能帮助我继续。

推荐答案

FragmentStatePageAdapter 需要照顾它的状态,请参见:<一href=\"https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/FragmentStatePagerAdapter.java\"相对=nofollow> FragmentStatePagerAdapter.java

FragmentStatePageAdapter takes care of it's state see: FragmentStatePagerAdapter.java

去最好的办法是在 onDestroyView 回调删除图像(例如,从适配器),并在 onCreatView 。片段和适配器将相应恢复它们的状态。

The best way to go is to remove images (eg. from adapter) in onDestroyView callback and reload them in onCreatView. Fragments and adapters would restore their state accordingly.

,唯一的缺点是,你不应该创建 FragmentStatePagerAdapter 在onStart 每一次,因为它不知道的保存的状态。

The only catch is that You should not create FragmentStatePagerAdapter every time in onStart because it will not be aware of saved state.

这篇关于保存状态和FragmentStatePagerAdapter还原状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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