Reputation: 609
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..
Upvotes: 14
Views: 7242
Reputation: 21
Additional explanation since this caused me to scratch my head for a few minutes:
You can create NavHostFragment programmatically using that code from OP's post and answer or you can put app:defaultNavHost="true"
into the fragment in your main activity.
Android Studio will make both of these ways when you create stub files. Either removing OP's code or that property will make it work.
Upvotes: 2
Reputation: 609
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()
}
Upvotes: 34