i have following xml:
<android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/apptheme.appbaroverlay"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" app:popuptheme="@style/apptheme.popupoverlay" /> </android.support.design.widget.appbarlayout> <!--i use include container framelayout below--> <!--<include layout="@layout/content_main" />--> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.improvemybrand.mainactivity" tools:showin="@layout/app_bar_main"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" /> </framelayout> <android.support.design.widget.floatingactionbutton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_input_add" /> and problem simple:
when try replace container framelayout coordinator, not work, shows new fragment keeps old one, in simple example, textview hello world remains.
to replace, i'm using following code:
fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.replace(r.id.content_main, fragment); transaction.commit(); any ideas?
fragmenttransactions deal fragments , views. textview you've defined in layout not (part of) fragment's view, not affected fragmenttransaction, , fragment in snippet added on top of it.
you have few options. hide or remove textview when performing transaction. might preferable, if simple textview need initially, , sticking in fragment overkill. set opaque background on fragment's layout, hide textview.
the best option, however, put textview in layout fragment loaded @ startup. subsequent transactions replace/remove you're expecting. note fragment wish replace/remove @ runtime must loaded dynamically in code. is, cannot defined in <fragment> elements in activity's layout.