Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Views added to a FragmentContainerView must be associated with a Fragment

Due to difficulties with the implementation of the back button in the toolbar between two fragments, I decided to start acquaintance with Navigation Graph. But I get an error

Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.RelativeLayout{...} is not associated with a Fragment.

This is .xml file with nav_host_fragment:

<?xml version="1.0" encoding="utf-8"?>
<layout ...
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"
        app:defaultNavHost="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorBlack"
            android:clickable="@{viewModel.isSearching ? false : true}"
            android:focusable="@{viewModel.isSearching ? false : true}">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recent_recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="4dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="4dp"
                android:layout_marginBottom="4dp"
                android:background="@color/colorBlack"
                tools:itemCount="10"
                tools:listitem="@layout/search_user_layout" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/search_recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="4dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="4dp"
                android:layout_marginBottom="4dp"
                android:background="@color/colorBlack"
                android:visibility="gone"
                tools:itemCount="10"
                tools:listitem="@layout/search_user_layout" />

            <ProgressBar
                android:id="@+id/loading"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:indeterminate="true"
                android:visibility="@{viewModel.isSearching ? View.VISIBLE : View.GONE}" />
        </RelativeLayout>
    </fragment>
</layout>

Is it possible to fix it?

like image 962
MaxB Avatar asked Jul 03 '20 20:07

MaxB


People also ask

What is a fragment container view?

FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout , so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.

Why do we prefer FrameLayout for fragments?

Main purpose of frame layout is to block the area required to fit the largest child view. If you use a Frame Layout as Fragment Container you can ensure that you always have the space available to accommodate the largest fragment layout.

How do I use FragmentContainerView?

Another way to simply add a Fragment to FragmentContainerView is by using the attribute android:name=”fragment_class_name_path" in XML. Both the attributes android:name or class are similar things we just need to give the classpath as a value to inflate the fragment.


1 Answers

Check your Fragment's onCreateView() Layout inflate . Make sure to set attachToRoot false

inflater.inflate(R.layout.fragment_layout, container, false)

It's works for me.

like image 109
Ruben Baghajyan Avatar answered Oct 25 '22 03:10

Ruben Baghajyan