Child fragment replacing parent fragment root layout
android, android-fragments, android-viewpager
Solution
The `replace` transaction doesn't remove the current views from the target layout container so when you use the first pice of code the new `Fragment` is added to the contacts_layout `LinearLayout` but it will not be seen as the previous views cover the entire screen(height).
With the second piece of code the `LinearLayout` to which you add the new `Fragment` is the first child of the parent `LinearLayout` and it has space so its visible.
For what you're doing I advise you to wrap the initial layout in a `Fragment` class placed in a wrapper layout which you could then later replace very easy.
Problem
I've viewpager with 5 fragments, in one of them I want to completely replace it by button click. I also want to be able to hide child fragment by back button. Here this fragment layout: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/contacts_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null"> <LinearLayout android:id="@+id/import_contacts" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <include layout="@layout/listview_filter"/> <Button android:id="@+id/btn_my_clusters" android:background="@drawable/btn_wide_arrow_bg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/TEXT" android:text="@string/btn_my_clusters_text" android:layout_marginBottom="10dp"/> <ProgressBar android:id="@+id/progress_bar" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_gravity="center"/> <ListView android:id="@+id/contacts_list" android:divider="@null" android:listSelector="@android:color/transparent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone"/> </LinearLayout> ``` When I try replace `contacts_layout` like this: ``` ImportContactsFragment importContactsFragment = new ImportContactsFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.addToBackStack(null); transaction.replace(R.id.contacts_layout, importContactsFragment).commit(); ``` it doesn't work (I mean there is no error, but my ImportContactsFragment not showing at all). But when I try replace `import_contacts` view like this: ``` ImportContactsFragment importContactsFragment = new ImportContactsFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.addToBackStack(null); transaction.replace(R.id.import_contacts, importContactsFragment).commit(); ``` everything is ok, ImportContactsFragment shown. So I wonder is it possible to replace all fragment content by child fragment? Maybe I can do it in some other way?