当屏幕方向改变时,不同的片段未加载(Android)

6

我创建了一个测试项目,其中包含两个不同的片段,可以在同一个活动中显示。一个片段用于横屏,另一个片段用于竖屏。

# My unique activity
public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

# First Fragment
public class LandscapeFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
        v.setText("LANDSCAPE");
        return v;
    }
}

# Other Fragment
public class PortraitFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
        v.setText("PORTRAIT");
        return v;
    }
}

我有两个main.xml文件,一个在layout/目录下,另一个在layout-land/目录下。每个main.xml都指向正确的要使用的Fragment。

<!-- layout-land/main.xml  -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment"
    android:name="br.luckcheese.test.LandscapeFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp" />

<!-- layout/main.xml  -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment"
    android:name="br.luckcheese.test.PortraitFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp" />

<!-- layout/fragment.xml  -->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp" />

当我在横屏模式下打开应用程序时,将显示LandscapeFragment;当我在竖屏模式下打开应用程序时,则会显示PortraitFragment。到目前为止,一切都很好。
但是,如果我在横屏模式下打开应用程序,并将设备旋转到竖屏模式,则LandscapeFragment会被重新加载并显示。这不是预期的行为,应该加载PortraitFragment。
同样的事情也会发生在另一个方向上,如果设备以竖屏方向启动,然后我将其旋转到横屏模式:PortraitFragment只会被重新加载,而不是加载LandscapeFragment。
我做错了什么?
2个回答

2

请确保您的manifest.xml中没有android:config="orientation"


不,我添加到默认清单中的唯一行是“android:screenOrientation =”sensor“”用于活动。 - Luckcheese
你是否尝试在活动的onCreate()方法中设置断点,以查看在旋转设备时是否调用了该方法? - Nhat Nam NGUYEN
是的,它被称为“活动重建”。我在我的活动和每个片段的onCreate中添加了日志消息。当我旋转设备时,在活动的onCreate之后调用了错误片段的onCreate。 - Luckcheese

1
问题出在你的两个布局上,片段 ID 是相同的。 只需更改这些片段 ID,例如:

layout/main.xml 的片段 ID 为 fragmentportrait

layout-land/main.xml 的片段 ID 为 fragmentlanscape。 (还要更改: Fragment frag = getSupportFragmentManager().findFragmentById(R.id.fragment);

frag.setRetainInstance(false); )


好的,那个工人。但我不明白为什么需要添加不同的ID?因为在我的代码中,我必须测试两者才能设置frag.setRetainInstance(false)。但是,我也通过使用FrameLayout创建唯一布局,并在测试当前方向getResource().getConfiguration().orientation后通过代码添加片段来实现工作! - Luckcheese

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接