我有一个FragmentActivity
和两个Fragment
s,说FrgMaster
(a ListFragment
)和FrgDetail
。我有两个布局XML文件:一个包含单个FrameLayout
(用于纵向模式),另一个包含两个FrameLayout
(用于横向)。我想实例化我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();
}
我浏览列表,点击的地方。现在,当我改变方向时,上面的代码不起作用,因为savedInstanceState
不是null
;因此片段不会添加到布局中。如果我删除if
条件,则最终会生成多个片段,每个片段对应于一个方向更改,堆叠。
我错过了什么?
“现在,当我改变方向时,上面的代码不起作用,因为savedInstanceState不为空;因此片段不会添加到布局中“ - 它应该作为正常配置更改处理的一部分自动重新添加到布局。见https://github.com/commonsguy/cw-omnibus/tree/master/LargeScreen/EU4You – CommonsWare 2013-03-05 00:51:57
我不确定你的意思。你提出另一种方法?这是我不应该做的事情? – denispyr 2013-03-05 00:58:12
不,更多的是你可能应该工作,但我倾向于使用科里斯科特在他的答案中。 – CommonsWare 2013-03-05 01:07:09