2014-05-01 25 views
0

我有上有四個片段的形式的活動:旋轉不會取代片段

----------- 
| 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(); 
} 

佈局\ 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> 

佈局,土地\ 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> 

回答

1

唐在方向改變時不會替換片段。只需檢查保存的實例是否爲空,然後添加片段並提交。

當活動在定位更改上重新創建時,片段管理器將保留片段實例,並調用onCreateView。

希望這個工程。讓我知道 。

+0

就是這樣,謝謝! – TheRedAgent