片段和处理方向变化 [英] fragments and handling orientation changes

查看:151
本文介绍了片段和处理方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有中有一个片段的活动。​​

i have an activity with a fragment in it.

我想处理的方向改变自己,所以我更新清单看起来是这样的:

I would like to handle the orientation change myself, so i updated the manifest to look like this:

    <activity android:name="com.test.app" android:configChanges="orientation|keyboardHidden"/>

然后我更新了活动如下:

Then i updated the activity to look like this:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        updateLayout();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        updateLayout();
    }

    private void updateLayout() {
        setContentView(R.layout.my_layout);
    }

我也是这样做的片段:

I also do this with the fragment:

    fragment.setRetainInstance(true);

我的问题是,当我做屏幕方向,它失败的的setContentView()说,有我的片段重复的ID。不知道如何让这个不会发生 - 想法

The issue I have is that when I do the screen orientation, it fails on the setContentView() saying that there is a duplicate id for my fragment. Not sure how to make it so that doesn't happen -- ideas?

TIA。

推荐答案

我相信这是因为你告诉它不要扔掉你的previous布局,所以当你转动它,你还有你的旧观点(这在这种情况下,是一样的新的视图,因此ID冲突)。

I believe it's because you told it not to throw away your previous layout, so when you rotate it, you still have your old view (which in this case, is the same as the new view, thus the ID conflicts).

另外,我不知道,但我觉得这样的:

Also, I'm not sure, but I think this:

fragment.setRetainInstance(true);

是不必要?因为在这里你告诉它不要在重新配置更改您的活动:

is unnecessary? Because here you tell it NOT to recreate your activity on configuration changes:

android:configChanges="orientation|keyboardHidden"

在我的经验,在configChanges设置XML就足以prevent娱乐。

In my experience, the configChanges setting in XML is enough to prevent recreation.

编辑:

嗯只需再次寻找,究竟您使用的片段?如果在此发布的code是从你的FragmentActivity,那么我希望这样的事情对你的充气片段并将其添加到活动:

Mmmm just looking again, how exactly are you using Fragments? If the code posted here is from your FragmentActivity, then I would expect something like this for inflating your Fragment and adding it to the Activity:

class SomeActivity extends FragmentActivity
{
    ...
    @Override
    public void onCreate( Bundle savedInstance )
    {
    ...
        LayoutInflater inflater = getLayoutInflater();
        inflater.inflate( R.layout.some_fragment, root );
    ...
    }
}

使用该XML看起来像:
some_fragment.xml

With that XML looking something like: some_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="com.someapp.fragments.SomeFragment">
</fragment>

所以,我想我不会对你如何使用片段清楚。但使用起来像这样,使用XML配置设置,成功地禁用旋转娱乐我。

So I guess I'm not clear on how you are using Fragments. But using them like this, with the XML config setting, successfully disables recreation on rotation for me.

这篇关于片段和处理方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆