Why does it show the activity_main.xml instead of the start fragment using Navigation Component

Viewed 39

I've been trying to get my hands around the navigation component but for some reason when I run my app it doesnt show the expected xml and I'm not able to figure out why this happens (The expected xml is the one called fragment_inicio, but it shows the activity_main.xml)

Here is the code:

MainActivity

package com.example.practica1

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController

class MainActivity : AppCompatActivity(R.layout.activity_main) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController

        setupActionBarWithNavController(navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        return super.onSupportNavigateUp() || super.onSupportNavigateUp()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

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

</androidx.constraintlayout.widget.ConstraintLayout>

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.xml"
    app:startDestination="@id/inicioFragment">

    <fragment
        android:id="@+id/inicioFragment"
        android:name="com.example.practica1.InicioFragment"
        android:label="fragment_inicio"
        tools:layout="@layout/fragment_inicio" >
        <action
            android:id="@+id/action_inicioFragment_to_menuFragment"
            app:destination="@id/menuFragment" />
    </fragment>
    <fragment
        android:id="@+id/cartoonFragment"
        android:name="com.example.practica1.CartoonFragment"
        android:label="fragment_cartoon"
        tools:layout="@layout/fragment_cartoon" />
    <fragment
        android:id="@+id/adultSwimFragment"
        android:name="com.example.practica1.AdultSwimFragment"
        android:label="fragment_adult_swim"
        tools:layout="@layout/fragment_adult_swim" />
    <fragment
        android:id="@+id/menuFragment"
        android:name="com.example.practica1.MenuFragment"
        android:label="fragment_menu"
        tools:layout="@layout/fragment_menu" >
        <action
            android:id="@+id/action_menuFragment_to_cartoonFragment"
            app:destination="@id/cartoonFragment" />
        <action
            android:id="@+id/action_menuFragment_to_adultSwimFragment"
            app:destination="@id/adultSwimFragment" />
    </fragment>
</navigation>

Build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    buildFeatures {
        dataBinding true
    }

    defaultConfig {

        configurations.all {
            resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
        }

        applicationId "com.example.practica1"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    def nav_version = "2.3.4"
    def lifecycle_version = "2.3.1"
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation 'com.google.android.material:material:1.4.0'
    implementation "androidx.core:core-ktx:+"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

}

repositories {
    mavenCentral()
}

Thanks!

1 Answers

you have to remove the id layout from the AppCompatActivity()

class MainActivity : AppCompatActivity(R.layout.activity_main) {

to

class MainActivity : AppCompatActivity() {

    val binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

You have to inflate the layout via the ActivityMainBinding

Related