简体   繁体   English

使用具有不同布局的纵向和横向片段处理活动

[英]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. 我有一个带有FragmentActivity ,并且它具有用于横向和纵向模式的不同布局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-landlayout-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.

相关问题 创建用于纵向活动的“横向”布局 - Create “Landscape” Layout For Use in Portrait Activity 如何在flutter中显示横向和纵向的不同布局 - How to show different layout for landscape and portrait in flutter 纵向和景观的不同的网格布局在bootstrap - Different grid layout for portrait and Landscape in bootstrap Android 注释:纵向和横向的不同布局 - Android annotations: Different layout for portrait and landscape Android布局横向/纵向具有不同的选项 - android layout landscape/portrait with different options Android布局在横向和纵向上具有不同的项目 - Android layout with different items on landscape and on portrait Android:活动中的屏幕旋转不会切换纵向/横向布局 - Android: Screen Rotation within Activity doesnt switch portrait/landscape layout 如何处理Android Activity.java文件的layout和layout-landscape中的不同初始化变量? - How to handle different initialized variables in layout and layout-landscape for android activity.java file? 当横向活动开始时,肖像布局不起作用 - Portrait layout doesnt work when activity starts in landscape Landscape to Portrait重现活动? - Landscape to Portrait recreates activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM