6
votes

I'm trying to implement the new Android Navigation Component in an existing app. I have 2 fragments that are identical except for their name. When I set the startDestination to fragment2, the fragment seems to be shown correctly. When the startDestination is set to fragment1, I don't see the inflated view but I do see the "Fragment 1 created" toast.

What am I doing incorrectly?

class Fragment1 : Fragment() {

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?): View? {
    Toast.makeText(context, "Fragment 1 created", Toast.LENGTH_LONG).show()
    return inflater.inflate(R.layout.fragment1, container, false)
  }

}

class Fragment2 : Fragment() {

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?): View? {
    Toast.makeText(context, "Fragment 2 created", Toast.LENGTH_LONG).show()
    return inflater.inflate(R.layout.fragment2, container, false)
  }


}

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/fragment1">

  <fragment
      android:id="@+id/fragment1"
      android:name="com.package.anotherpackage.ui.Fragment1"
      android:label="fragment1"
      tools:layout="@layout/fragment1"/>

  <fragment
      android:id="@+id/fragment2"
      android:name="com.package.anotherpackage.ui.Fragment2"
      android:label="fragment2"
      tools:layout="@layout/fragment2"/>
</navigation>

MainActivity layout:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    tools:context=".MainActivity">

  <fragment
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/nav_host_fragment"
      android:name="androidx.navigation.fragment.NavHostFragment"
      app:navGraph="@navigation/nav_graph"
      app:defaultNavHost="true"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      />

</FrameLayout>

Fragment1 Layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.Fragment1"
    >

  <!-- TODO: Update blank fragment layout -->
  <TextView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="fragment 1"
      />

</FrameLayout>

Dependencies:

//Navigation
  def nav_version = "1.0.0-alpha05"

  implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
  implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"

  // optional - Test helpers
  androidTestImplementation "android.arch.navigation:navigation-testing-ktx:$nav_version"
1
If you change your Fragment1 to inflate R.layout.fragment2 (just for testing), does the Fragment2 layout appear alongside the Fragment1 Toast message? If so, please include your R.layout.fragment1 layout, which is most certainly the cause of your problemianhanniballake
Yes, I see the fragment2 layout along with the Fragment1 toast. I edited the post and added the fragment1 layout. Thank you for the help!Evan M
Your comment led me to my solution. Another layout in my project (different library) was conflicting with the layout I was trying to inflate in Fragment1. Thanks again.Evan M

1 Answers

3
votes

I had a layout in another library in my project with the name "fragment1.xml". It must have been conflicting with the other layout I was trying to inflate in Fragment1. Renaming the Fragment1 layout fixed the issue.