3

Am new with this architecture and am trying to add multiple fragments in the Android Navigation component but the Fragments are not visible in the list.

enter image description here

As you can see, The host is the only one available.

I also had a look at this question, but it didn't help me out.

Now, these are the dependencies am using from the documentation :

def nav_version = "2.1.0-beta02"
    def nav_version_ktx = "2.1.0-beta02"

     // Java
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version_ktx"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version_ktx"

and this :

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

Then my fragment is as follows :

class BlankFragment2 : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank_fragment2, container, false)
    }


}

and it's xml :

<?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=".BlankFragment2">

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

</FrameLayout>

The version of my android studio is 3.4.2

What could be the reason to this ?

Idris Stack
  • 546
  • 1
  • 14
  • 36

4 Answers4

1

I am using the version 2.1.0-alpha02 and have no problems. Try to change your nav_version_ktx and nav_version

Also this a sample on my github, just in case you want to check something. But i think it's a problem of version

0

Well, I got the solution, I just migrated my app to use Androidx, this made the graph work as expected.

Idris Stack
  • 546
  • 1
  • 14
  • 36
0

In your nav graph add "tools:layout="@layout/fragment_your_layout"

<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/main_navigation"
    app:startDestination="@id/home2Fragment">

    <fragment
        android:id="@+id/newProfileContentFragment"
        android:name="com.adidas.app.profile2.NewProfileContentFragment"
        android:label="NewProfileContentFragment"
        tools:layout="@layout/fragment_your_layout"
        />

</navigation>
juanmeanwhile
  • 2,594
  • 2
  • 24
  • 26
0

check if you have NavigationComponent dependency in your build.gradle.

 ext.navigation_version = "2.3.5"

 // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$navigation_version"
    androidTestImplementation "androidx.navigation:navigation-testing:$navigation_version"
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44