[英]Android Fragment isAdded returns false after configuration change (screen Rotation)
I have an Activity with a ViewPager2.我有一个带有 ViewPager2 的活动。 A ScreenSlidePagerAdapter is creating my 4 Fragments in the ViewPager.
ScreenSlidePagerAdapter 正在 ViewPager 中创建我的 4 个片段。 After sliding through the fragments
isAdded
returns true
for every Fragment.滑过片段后,
isAdded
为每个片段返回true
。 After a screen rotation configuration change isAdded
returns false
for all Fragments, but I have to keep them added.在屏幕旋转配置更改后,
isAdded
为所有片段返回false
,但我必须保持添加它们。 Is there a possibility to re-add them, or how can I keep them added?是否有可能重新添加它们,或者我如何保持添加它们?
UPDATE 1: after the configuration change isAdded
is only false
for all fragments which I slided through before the configuration change.更新 1:配置更改后
isAdded
仅对于我在配置更改之前滑过的所有片段都是false
的。 For the other Fragments isAdded
gets true
also after the configuration change.对于其他 Fragments,
isAdded
在配置更改后也会变为true
。
The Adapter code is:适配器代码是:
private inner class ScreenSlidePagerAdapter(fa: FragmentActivity, data: Bundle) : FragmentStateAdapter(fa) {
val fragmentBundle: Bundle = data
override fun createFragment(position: Int): Fragment {
when (position) {
1 -> {
sliderFragment2.arguments = fragmentBundle
return sliderFragment2
}
2 -> {
sliderFragment3.arguments = fragmentBundle
return sliderFragment3
}
3 -> {
sliderFragment4.arguments = fragmentBundle
return sliderFragment4
}
else -> {
sliderFragment1.arguments = fragmentBundle
return sliderFragment1
}
}
}
override fun getItemCount(): Int {
return NUM_PAGES
}
}
}
public class TestActivity extends FragmentActivity{ @Override public void onCreate(Bundle savedInstanceState){ //Use null, no restore super.onCreate(null); ... } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.