Fragment || onConfigurationChanged || Duplicate

http://stackoverflow.com/questions/9415574/fragment-onconfigurationchanged-duplicate


在学习Android Application Development Cookbook 93 Recipes For Building Winning Apps的第Recipe 3.10时候,在真机上如果想转屏显示,总会出现duplicate id问题导致应用挂了。

Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f090008, tag null, or parent id 0xffffffff with another fragment for com.febro.myfragmenttest.ListFragment
难道说写成android_landscape_main.xml和android_main.xml在 onConfigurationChanged的时候没有调用

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
		
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_main);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.activity_landscape_main);
        }
	}
在反复调试之后,发现不是这里的问题。


最后在stackoverflow上找到了答案。

You should let android do all that configuration switching for you! Don't do anything in "onConfigurationChanged", remove all configuration changes from your manifest even!

Put your portrait layout in layout/ folder

and put your landscape layout in layout-land/ folder

Make sure they are both named: main.xml (or anything, as long as it is the same)

put your portrait layout in /layout/ and your landscape layout in /layout-land/. They can be completely different, just need the same name. To save and restore data, save your data in onSaveInstanceState and restore it in in onCreate when the Bundle != null 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值