Android ViewPager orientation change(Android ViewPager 方向改变)
问题描述
我已阅读 @antonyt 的 对此 StackOverflow 问题,FragmentPagerAdapter 将在方向更改时尝试重用 FragmentManager.findFragmentByTag() 找到的现有片段.
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.
我已经试过了.FragmentPagerAdapter 的 getItem(int) 在第一次需要 Fragment 时被调用.在方向改变时,getItem 不会被调用,这与 @antonyt's 的回答一致.但是为什么又调用了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 方向改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android ViewPager 方向改变
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
