Android ViewPager方向更改 [英] Android ViewPager orientation change

查看:375
本文介绍了Android ViewPager方向更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已从 @antonyt的答案中阅读了此

I have read from @antonyt's answer to this StackOverflow question that FragmentPagerAdapter will try to reuse an existing fragment found by FragmentManager.findFragmentByTag() upon orientation change.

我已经尝试过了.第一次需要Fragment时会调用FragmentPagerAdaptergetItem(int).更改方向时,不会调用getItem,这与 @antonyt的答案一致.但是为什么再次调用Fragment中的onCreate()?我以为它没有被摧毁?

I have tried this. getItem(int) of the FragmentPagerAdapter is called when the Fragment is first needed. Upon orientation change, getItem is not called, which is in accordance with @antonyt's answer. But why is it that onCreate() of the Fragment is called again? I thought it was not destroyed?

推荐答案

所有未设置setRetainInstance(true)的片段都将在方向更改时销毁.更改方向后,所有片段都会被片段管理器重新创建并重新附加(因此片段会贯穿整个生命周期).

All fragments which not set setRetainInstance(true) are destroyed on orientation change. After orientation changed all fragments are recreated and reattach by fragment manager (so fragment goes throught entire lifecycle).

ViewPager附加的所有片段都将添加到带有特殊标记的片段管理器中. ViewPager尝试重用在方向更改后重新创建的片段(通过在片段管理器中搜索标签).如果这样的片段不存在,则调用FragmentPagerAdapter.getItem(int)来创建新的片段.

All fragment attached by ViewPager are added to fragment manager with special tag. ViewPager try to reuse fragment recreated after orientation changed (by searching for tag in fragment manager). If such fragment don't exist then FragmentPagerAdapter.getItem(int) is called to create new fragment.

这篇关于Android ViewPager方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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