1

I'm creating an application where it uses the Navigation Architecture, but my navigation has no default host, and can be set based on the selected destination by the user.

So this is the setup of my NavHostFragment

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var finalHost: NavHostFragment? = null
        when (bundle) {
            REQUEST_FIRST -> {
                finalHost = NavHostFragment.create(R.navigation.navigation_first)
            }
            REQUEST_SECOND -> {
                finalHost = NavHostFragment.create(R.navigation.navigation_second)
            }
            REQUEST_THIRD -> {
                finalHost = NavHostFragment.create(R.navigation.navigation_third)
            }
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragmentMain, finalHost!!)
            .setPrimaryNavigationFragment(finalHost)
            .commit()
}

and this is my main activity layout.

<?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"
    android:background="@color/black">
    <!-- some stuff -->
    <fragment
            android:id="@+id/fragmentMain"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

So, my MainActivity is loading correctly but when I start to rotate the screen of the device, It encounters some errors.

java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment

the said error is pointing on my setContentView(R.layout.activity_main) and the NavHostFragment <fragment> in layout.

any help is appreciated, Thanks!

dotGitignore
  • 1,485
  • 1
  • 12
  • 31
  • there is strong possibility that after rotating screen your bundle value is getting erase or may be null – Kalpesh Kulye Aug 14 '19 at 08:22
  • https://stackoverflow.com/q/12672972/7592063. Go through this Q&A. Instead of using Fragmentmanager use findNavController for navigation. Read this document https://developer.android.com/guide/navigation/navigation-getting-started#navigate – Rajnish suryavanshi Aug 14 '19 at 08:27

0 Answers0