我已阅读 @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 方向改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!