【问题标题】:Rotation won't replace Fragments旋转不会取代碎片
【发布时间】:2014-05-01 20:16:06
【问题描述】:

我有一个活动,上面有四个片段,形式为:

 -----------  
|  1  |  2  |
 -----------
|     3     |
 -----------
|     4     |
 -----------

当我旋转屏幕时,片段应该是这样的:

 -----------
|   | 2 |   |
| 1 |---| 4 |
|   | 3 |   |
 -----------

我已经为横向和纵向等正确创建了布局文件,如果我以任一方向启动应用程序,它都可以正常工作。但是,当我旋转应用程序时,我会丢失 一些 片段。例如,如果我从纵向模式开始并旋转到横向模式,片段 2 和 3 现在是空白且不显示,如果我旋转回纵向模式,应用程序会崩溃,因为它无法再找到这些片段添加回应用程序。
这是我的 onCreate 和 XML 文件中的代码。非常感谢任何帮助!

Main.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
        ssf = new StartStopFragment();
        tkf = new TimeKeeperFragment();
        rf = new ResetFragment();
        lcf = new LapCounterFragment();
    } else {
        ssf = (StartStopFragment) getFragmentManager().findFragmentByTag(StartStopFragment.class.getName());
        tkf = (TimeKeeperFragment) getFragmentManager().findFragmentByTag(TimeKeeperFragment.class.getName());
        rf = (ResetFragment) getFragmentManager().findFragmentByTag(ResetFragment.class.getName());
        lcf = (LapCounterFragment) getFragmentManager().findFragmentByTag(LapCounterFragment.class.getName());
    }

    getFragmentManager().beginTransaction()
            .replace(R.id.flStartFrame, ssf, StartStopFragment.class.getName())
            .replace(R.id.flTimeKeeperFrame, tkf, TimeKeeperFragment.class.getName())
            .replace(R.id.flResetFrame, rf, ResetFragment.class.getName())
            .replace(R.id.flLapCounterFrame, lcf, LapCounterFragment.class.getName())
            .commit();
}

layout\main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">
    <LinearLayout
        android:id="@+id/llTopContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".4"
        android:orientation="horizontal"
        android:baselineAligned="false">
        <FrameLayout
            android:id="@+id/flResetFrame"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            tools:layout="@layout/fragment_reset"/>
        <FrameLayout
            android:id="@+id/flLapCounterFrame"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            tools:layout="@layout/fragment_lapcount"/>
    </LinearLayout>
    <FrameLayout
        android:id="@+id/flTimeKeeperFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".2"
        tools:layout="@layout/fragment_time"/>
    <FrameLayout
        android:id="@+id/flStartFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".4"
        tools:layout="@layout/fragment_startstop"/>
</LinearLayout>

layout-land\main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">
    <LinearLayout
        android:id="@+id/llTopContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".4"
        android:orientation="horizontal"
        android:baselineAligned="false">
        <FrameLayout
            android:id="@+id/flResetFrame"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            tools:layout="@layout/fragment_reset"/>
        <FrameLayout
            android:id="@+id/flLapCounterFrame"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            tools:layout="@layout/fragment_lapcount"/>
    </LinearLayout>
    <FrameLayout
        android:id="@+id/flTimeKeeperFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".2"
        tools:layout="@layout/fragment_time"/>
    <FrameLayout
        android:id="@+id/flStartFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".4"
        tools:layout="@layout/fragment_startstop"/>
</LinearLayout>

【问题讨论】:

    标签: android android-fragments screen-rotation


    【解决方案1】:

    不要在方向改变时替换片段。只需检查保存的实例是否为空,然后添加片段并提交。

    当 Activity 在方向改变时重新创建时,片段管理器将保留片段实例并调用 onCreateView。

    希望这可行。让我知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多