Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Navigation: NavHostManager is not an active fragment of FragmentManager

I'm using Jetpack Navigation for handling the navigation for Fragments.
I've been following the documentation and installed the required components, still the app just crash when trying to display the activity hosting the NavHost fragment

Exception:

java.lang.IllegalArgumentException: Fragment NavHostFragment{820022f} is not an active fragment of FragmentManager FragmentManager{5a5703c in HostCallbacks{a0b41c5}}
        at android.support.v4.app.FragmentManagerImpl.setPrimaryNavigationFragment(FragmentManager.java:3389)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:783)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)

Main Activity Layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity" >

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

Main Activity - Kotlin

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_activity)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                    .replace(R.id.container, LoginFragment.newInstance())
                    .commitNow()
        }
    }

    override fun onSupportNavigateUp()
        = findNavController(R.id.my_nav_host_fragment).navigateUp()

}

I'm trying to test JetPack features, and I'm currently stuck on this one, anyone has a clue what might be wrong ? Is it due to Android 3.2 being still in preview ? Any help would be much appreciated..

like image 808
sallah kokaina Avatar asked May 17 '18 14:05

sallah kokaina


1 Answers

Solved.

The issue came from the call to the FragmentManager to replace its content with a new instance of the LoginFragment, simply removing the following piece of code from the onCreate method did solve it

if (savedInstanceState == null) {
    supportFragmentManager.beginTransaction()
            .replace(R.id.container, LoginFragment.newInstance())
            .commitNow()
}
like image 181
sallah kokaina Avatar answered Sep 16 '22 12:09

sallah kokaina