简体   繁体   English

屏幕旋转后ViewPager Fragments消失

[英]ViewPager Fragments disappear after screen rotation

I have the following problem: 我有以下问题:

The content of my fragments is dependet on the content of xml files. 我的片段的内容取决于xml文件的内容。 So in the onStart method of my FragmentActivity I call a method which reads those files and generates fragments and puts them in a list. 因此,在我的FragmentActivityonStart方法中,我调用一个方法来读取这些文件并生成片段并将它们放入列表中。 These fragments hold an ArrayList which contains all the views to display in this fragment. 这些片段包含一个ArrayList,其中包含要在此片段中显示的所有视图。

After generating the fragment-list, I give it into a FragmentPagerAdapter to display them in a ViewPager . 生成片段列表后,我将其放入FragmentPagerAdapter以在ViewPager显示它们。 At this point, when connecting the ViewPager to the adapter, the onCreateView method of my fragments is called. 此时,将ViewPager连接到适配器时,会调用片段的onCreateView方法。 This is where the views from the above mentioned ArrayList are placed on the containerview. 这是上面提到的ArrayList的视图放在containerview上的地方。

Thats how it works if the activity is created initially. 这就是如果最初创建活动它是如何工作的。

When it's restoring it's state, for example after a rotation change, the onCreateView method of my fragments is called before I can even parse the xml files and generate the views to display in my fragments. 当它恢复它的状态时,例如在轮换更改之后,我的片段的onCreateView方法被调用,然后我甚至可以解析xml文件并生成要在我的片段中显示的视图。 Therefore the fragments are empty. 因此碎片是空的。

Try to add 尝试添加

android:configChanges="keyboardHidden|screenSize|orientation"

to your manifest (inside your activity's definition). 到您的清单(在您的活动定义内)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM