简体   繁体   中英

Android Navigation Graph not displaying start fragment

I'm running into a wall debugging what should be a simple issue: I have an app with one activity, which contains a navigation graph, which should display a fragment on start. But it isn't.

activity_main.xml

<?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"
    tools:context=".MainActivity"
    >

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

</FrameLayout>

res/navigation/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/listFragment"
    >

    <fragment
        android:id="@+id/listFragment"
        android:name="org.example.my_app.ui.ListFragment"
        android:label="fragment_list"
        tools:layout="@layout/fragment_list"
        >
        <action
            android:id="@+id/action_listFragment_to_detailFragment"
            app:destination="@id/detailFragment"
            />
    </fragment>
    <fragment
        android:id="@+id/detailFragment"
        android:name="org.example.my_app.ui.DetailFragment"
        android:label="fragment_detail"
        tools:layout="@layout/fragment_detail"
        />
</navigation>

fragment_list.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=".ui.ListFragment"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="This should be visible"
        />

</FrameLayout>

ListFragment.kt

class ListFragment : Fragment(R.layout.fragment_list)

Despite this dead-simple setup, I see a blank screen (just an app bar) and debug listeners added to the ListFragment class init don't ever get called. Why isn't my navGraph initializing an instance of the fragment?

Documentation:

NavHostFragment provides an area within your layout for self-contained navigation to occur.

You are missing to declare the fragment placeholder as a NavHostFragment in the FragmentContainerView via the android:name attribute; and therefore the navigation won't occur.

<androidx.fragment.app.FragmentContainerView
    ....
    android:name="androidx.navigation.fragment.NavHostFragment"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM