所以我仍然在努力解决Fragments
和方向的变化。我在左侧有一个Activity
主ListFragment
,可以在右侧的FrameLayout
中加载所选操作,也可以启动新活动(非常标准,我正在尝试使用{ {3}}作为指南),使用函数。
问题在于,每次从纵向移动到横向时,都会创建一个新的Fragment
,但旧的replace(...)
不会被替换,尽管使用onStart()
。
这是函数的代码,当选择列表中的项目时,或者在MainActivity的public void startDataOp(int opId) {
FrameLayout flright = (FrameLayout)findViewById(R.id.iManageDataActionFrameLayout);
currentDataOp = opId;
if(orientation.equals("default")) { // start intents for each data operation, orientation is set earlier
switch (opId) {
case -1:
{ /* nothing to do here */ }
break;
case 0: {
Intent intent = new Intent();
intent.setClass(this.getApplicationContext(), AImportFile.class);
startActivityForResult(intent, 313);
}
break;
// ...
default:
break;
}
}
else { // landscape, so load them in the FrameLayout: iManageDataActionFragment
int flrichi = -1;
if(flright != null) { flrichi = flright.getChildCount(); }
FragmentTransaction ft = getFragmentManager().beginTransaction();
switch (opId)
{
case -1:
{
if(flrichi != -1)
{
flright.removeAllViews();
if(flright.getVisibility() != View.GONE) { flright.setVisibility(View.GONE); }
}
}
break;
case 0:
{
if(flrichi == 0) { // this gets only called the first time
flright.setVisibility(View.VISIBLE);
ft.add(R.id.iManageDataActionFrameLayout, FImportFile.newInstance(counter, orientation), "tDataOpsRightAction");
}
else { // this gets called after the first time and yet the number of Fragments keeps increasing
ft.replace(R.id.iManageDataActionFrameLayout, FImportFile.newInstance(counter, orientation), "tDataOpsRightAction");
}
ft.addToBackStack(null);
ft.commit();
}
break;
// ...
default:
break;
}
}
}
上调用(没有所有调试行):
ft.replace(...)
请注意,Logcat确实表明选择了正确的选项({{1}}),并且在提交之前和之后,右侧FrameLayout的子计数为1。但是,LogCat告诉我,被替换的碎片仍在那里!
我错过了什么?
提前感谢您的帮助!