方向改变后没有碎片重新添加 [英] Fragment not re-added after orientation change

查看:92
本文介绍了方向改变后没有碎片重新添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序碎片不会当设备旋转重新添加到该活动的看法。如果我(在这里和类似的问题对SO)了解正确的文件,这应该是片段管理器处理(无需指定的android:configChanges在我的清单)。

The Fragments in my app are not re-added to the activity's view when the device is rotated. If i understand the documentation (and similar questions here on SO) correctly, this should be handled be the fragment manager (without having to specify android:configChanges in my manifest).

我的活动如下:

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

和一个片段看起来像(略clicklisteners等为清楚起见):

And a Fragment looks like (omitted clicklisteners and so on for clarity):

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

该布局文件的样子(activity_main.xml中):

The layout files look like (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>

和fragment_layout.xml:

And 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>

然而,当我转动我的设备屏幕总是变为空白。

However, when i rotate my device the screen always becomes blank.

推荐答案

原来我干了件蠢事。我推翻我的活动的onSaveInstanceState(...)方法,而不调用 super.onSaveInstanceState(),这导致在片段不被重建。在这里张贴的答案,希望我能拯救其他人的时候了!

Turns out I did a Stupid Thing. I overrode the onSaveInstanceState(...) method in my activity, without calling super.onSaveInstanceState(), which resulted in the fragments not being recreated. Posting the answer here in hopes that I can save somebody else the time!

这篇关于方向改变后没有碎片重新添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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