今天在使用Fragment的时候在xml添加了下面一段代码
<FrameLayout
android:id="@+id/frag_contain"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="9">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="mypro.app.com.mypro.Fragment.FirstPageFragment"
android:tag="f1"/>
</FrameLayout>
在onClick()方法中无论怎么调用
getFragmentManager().beginTranscation().remove(fragment).commit();
或者
replace(...)方法都没办法吧FrameLayout里面的fragment去掉。
后来在网上阅读Fragment文档的时候发现在xml中添加的Fragment是没办法被remove(...)或replace(...)掉的。
所以解决方法就是将上面的FrameLayout中的fragment去掉,在onCreate(),或者框架中初始化View的地方使用add(...),将初始的Fragment添加进去。
这时候就可以使用remove(...),replace(...)方法将它去掉用别的Fragment替代了