安卓:屏幕上的片段生命周期旋转



我知道http://developer.android.com/guide/components/fragments.html我想知道当我旋转屏幕并最终回到"Fragment Active"时,"FragmentActive"会发生什么。

我问题的背景是,我有一个应用程序,无论我是在纵向模式还是横向模式下启动,它都能正常工作。但在屏幕旋转时,它会转储

Fragment com.bla.bla did not create a view.

这个片段基本上只实现了onCreateView,没有其他

public View onCreateView(LayoutInflater i, ViewGroup c, Bundle s)
{
   return i.inflate(R.layout.mylayout, c, false);
}

知道屏幕旋转到底发生了什么,我希望能解决这个问题。。。

编辑:

我试了一下评论者的建议,更多的信息。所以他们基本上都建议有一个空的活动布局,如果我看对的话,可以通过编程添加片段。我有一个main.xml用于肖像和一个用于风景,两者现在看起来非常相似(区别是水平和垂直):

main.xml:

<LinearLayout xmlns:android="http:// and so on" 
    android:layout_width="fill_parent" 
    android:layout_heigt="wrap_content" 
    android:orientation=vertical" 
    android:id="@+id/myContainer">
</LinearLayout>

我的活动的onCreate方法如下:

super.onCreate(savedInstanceBundle);
setContentView(R.layout.main);
Fragment1 f1 = newFragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.myContainer, f1);
//and I need a second fragment
Fragment2 f2 = newFragment2();
ft.add(R.id.myContainer, f2);
ft.commit();

屏幕旋转似乎可以做到这一点(到目前为止,谢谢!)但在横向中,我只看到肖像中的第一个片段,我看到了两个片段,第二个片段多次出现(我旋转的频率越高,添加的频率就越高)。所以,要么我有布局问题,要么我不能像这样添加多个片段。仍在试图弄清楚这是否是布局问题,但还没有线索。有什么提示吗?

我理解你的问题的方式是,你不应该每次都添加片段。您应该用您的新片段替换当前存在的内容。

ft.replace(R.id.myContainer1, f1);
//and I need a second fragment
Fragment2 f2 = newFragment2();
ft.replace(R.id.myContainer2, f2);

至于Fragment的生命周期-当你旋转屏幕时,托管Activity被破坏并重新创建;所以直到CCD_ 3为止的所有正确的东西都应该被调用,然后是从CCD_ 4开始的所有东西。

最可靠的方法是覆盖所有的生命周期方法,并在所有方法中放入日志消息:-)

最新更新