【发布时间】:2014-01-06 23:34:27
【问题描述】:
我正在销毁一个以编程方式创建的片段:
getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.test)).commit();
在xml文件中是这样确定的:
<LinearLayout
android:id="@+id/test"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
如果我在 mainactivity 中再次从同一个类创建一个片段:
getSupportFragmentManager().beginTransaction()
.add(R.id.result_bar, testinstance)
.commit();
然后 onCreate 似乎没有再次调用(片段只是空的)。我在这里做错了什么?谢谢。
【问题讨论】:
-
我认为您应该尝试使用
FrameLayout而不是 LinearLayout。并因此删除android:visiblity="gone",您的布局将不会显示。 -
不,我在添加新片段时再次将可见性设置为可见
-
您尝试过 FrameLayout 吗?顺便说一句,你为什么不使用
replace()而不是remove() + add()? -
好吧(我的错...),LinearLayout 似乎也托管了一个片段。和所有 ViewGroup 一样,我猜。
-
感谢 cmets。出于某种原因,它适用于框架布局
标签: java android android-fragments fragmentmanager