我有 aFragmentActivity和两个Fragments,比如说FrgMaster(a ListFragment) 和FrgDetail。我有两个布局 XML 文件:一个包含一个FrameLayout(用于纵向模式),一个包含两个FrameLayouts(用于横向)。我想实例化我的片段,FragmentActivity所以onCreate()我有类似的东西:
if (savedInstanceState == null) {
final FrgMaster fragment = new FrgMaster();
// Add the fragment to the FrameLayout
this.getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_for_master, fragment, FrgMaster.MY_TAG)
.commit();
}
我浏览列表并单击位置。现在,当我改变方向时,上面的代码不起作用,因为savedInstanceStateis not null; 因此片段不会添加到布局中。如果我删除if条件,我最终会得到多个片段,一个用于每个方向变化的片段,堆叠在一起。
我错过了什么?