[英]Handle Activity with Fragments with different layout for portrait and landscape
I have an Activity
with Fragment
in it, and it has different layout xml files for landscape and portrait mode. 我有一个带有
Fragment
的Activity
,并且它具有用于横向和纵向模式的不同布局xml文件。
I try to use onConfigurationChanged()
like this: 我尝试像这样使用
onConfigurationChanged()
:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.hdmain);
}
Where hdmain.xml
is put in layout-land
and layout-port
folder. hdmain.xml
放在layout-land
和layout-port
文件夹中。 Then I meet the Fragment lifecycle problem: 然后我遇到了片段生命周期问题:
java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f0c0019, tag null, or parent id 0xffffffff with another fragment for org.sais.hdmeridian.HDMainActivity$TabFragment
It looks like the Fragment is still there, and since I changed the layout of Activity, a new Fragment is created then duplicated. 看起来片段仍然存在,并且由于我更改了活动的布局,因此创建了一个新的片段,然后进行了复制。
What's a smart way to avoid this? 避免这种情况的聪明方法是什么? Or does Android has some better solution to handle this problem?
还是Android有更好的解决方案来解决此问题?
Have you tried adding the tag android:configChanges="orientation"
to your Activity in the Manifest. 您是否尝试过将
android:configChanges="orientation"
到清单中的Activity中。 This prevents the onCreate to be executed on every turn of the devices, and solves most duplicate errors for me. 这样可以防止onCreate每次在设备上执行,并为我解决了大多数重复的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.