方向更改后不会重新添加片段

sgd*_*met 2 android android-fragments android-orientation

旋转设备时,我的应用中的片段不会重新添加到活动的视图中.如果我正确理解文档(以及SO上的类似问题),这应该是片段管理器处理(不必在我的清单中指定android:configChanges).

我的活动看起来像:

public class MainActivity extends SherlockFragmentActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null){
            getSupportFragmentManager().beginTransaction()
                .add(R.id.main_fragment_placeholder, new DashboardFragment(),"dashboard")
                .commit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

片段看起来像(为了清楚起见,省略了clicklisteners等):

public class DashboardFragment extends SherlockFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

布局文件看起来像(activity_main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/main.main">

    <FrameLayout android:id="@+id/main_fragment_placeholder"
             android:layout_height="fill_parent"
             android:layout_weight="1"
             android:layout_width="0px"
             />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

和fragment_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main.wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <my.layout.DashboardLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:id="@+id/main.dashboard">
    <!-- some buttons here -->
    </my.layout.DashboardLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

但是,当我旋转设备时,屏幕始终变为空白.

sgd*_*met 11

事实证明我做了一件愚蠢的事.我onSaveInstanceState(...)在我的活动中覆盖了方法,没有调用super.onSaveInstanceState(),这导致片段没有被重新创建.在这里发布答案希望我可以节省别人的时间!