I have read Documentation, Followed CodeLab, and watched youtube tutorials but whenever I try navigation on my own some or the other problem occurs,
This time my HomeFragment is not being hosted in the NavHost and I see a blank screen on launching the app.
I believe I have a Proper NavGraph
, NavHost
but I am not navigating yet so no NavController
.
Here are my files and classes:-
MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
activity_main.xml
<LinearLayout 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/myNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
navigation.xml
<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/navigation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/appInfoFragment"
android:name="com.example.themaze.AppInfoFragment"
android:label="fragment_app_info"
tools:layout="@layout/fragment_app_info" />
<fragment
android:id="@+id/homeFragment"
android:name="com.example.themaze.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_appInfoFragment"
app:destination="@id/appInfoFragment" />
</fragment>
</navigation>
fragment_home.xml
<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=".HomeFragment"
android:background="@color/teal_200">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/size"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home"
android:textSize="34sp" />
</FrameLayout>
It would be a great help if I get an answer as I have spent more than an hour figuring out the problem on the topic I thought I knew...
I think the problem lies in your activity_main.xml. Instead of using a fragment, please use a FragmentContainerView to host your fragment. Like this,
<androidx.fragment.app.FragmentContainerView
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" />
For more detail, check out https://developer.android.com/guide/navigation/navigation-getting-started#add-navhost