0

I have an application composed of one activity and several fragments, as recommanded by Google.

- MainActivity
  - LobbyFragment
  - GameFragment
  - ...

I also wanted to use the navigation map navigation map

I put a menu in the Activity to have everywhere, and then I would like to switch the content of the page.

To do this as shown in the fragment tutorial, i made the activitymain.xml to later switch the content of the FragmentContainerView between fragments.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:theme="@style/Theme.application.AppBarOverlay">
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.my.application.LobbyFragment" /> <!--Here is failure-->

</androidx.coordinatorlayout.widget.CoordinatorLayout>

But, at this point, when I compile, I have : Unable to start activity ComponentInfo{com.my.application/com.my.application.MainActivity}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class androidx.fragment.app.FragmentContainerView

And where to put the Activity in the navigation map ?

Zain
  • 37,492
  • 7
  • 60
  • 84
Foxhunt
  • 764
  • 1
  • 9
  • 23

1 Answers1

2

The documentation link you referenced uses the traditional fragment transaction by directly accessing supportFragmentManager; but it turns out from your post that you use Navigation Architecture components instead.

In navigation components, you are not supposed to use a particular fragment class in the android:name property, instead the navController takes care of that using NavHostFragment

To, fix this, please replace

android:name="com.my.application.LobbyFragment" 

With

android:name="androidx.navigation.fragment.NavHostFragment"

Also, you need to reference the name of the navGraph in that with app:navGraph attribute; assuming it's named as nav_graph:

app:navGraph="@navigation/nav_graph"
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thanks a lot ! It seemed to worked even with the good android:name only. What does `app:navGraph="@navigation/nav_graph"` do ? – Foxhunt Oct 28 '21 at 22:18
  • 1
    If i understand well, this specific navigation graph is used for this specific FragmentContainerView. So there is no use to place the activity in it ? – Foxhunt Oct 28 '21 at 22:23
  • @Foxhunt That is right, it's not referenced from the activity unless you need to programmatically modify some fragments in the graph; and that occurs in odd and advanced cases. – Zain Oct 28 '21 at 22:31
  • 1
    In your app, you could have another `FragmentContainerView` that references another `navGraph`, so a particular `FragmentContainerView` is tied to a single `navGraph` – Zain Oct 28 '21 at 22:43