I copied the NavigationExtensions.kt extension file from the NavigationAdvancedSample and I get this exception:
UPDATE: make sure your menu item ids are the same as your navigation resource files ids
also: if you're having a problem with ItemReselectedListener Add null safety checks, your listener should be something like this:
setOnNavigationItemReselectedListener { item ->
val newlySelectedItemTag = graphIdToTagMap[item.itemId]
val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
as NavHostFragment?
val navController = selectedFragment?.navController
// Pop the back stack to the start destination of the current navController graph
navController?.popBackStack(
navController.graph.startDestination, false
)
}
In my case, I forgot to add android:name="androidx.navigation.fragment.NavHostFragment" in FragmentContainerView
UPDATE: make sure your menu item ids are the same as your navigation resource files ids
also: if you're having a problem with ItemReselectedListener Add null safety checks, your listener should be something like this:
setOnNavigationItemReselectedListener { item ->
val newlySelectedItemTag = graphIdToTagMap[item.itemId]
val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
as NavHostFragment?
val navController = selectedFragment?.navController
// Pop the back stack to the start destination of the current navController graph
navController?.popBackStack(
navController.graph.startDestination, false
)
}
I also faced this probelem, the solution is actually very simple.
Make sure your menu item's id match the id of NAV_GRAPH not destinations.
I was using the same ids for menu items and destinations, but in this case, menu item should have same id as of NAV_GRAPH.
I also had the same issue
Check that you have set your start destination and attached it to a fragment. in your nav graph set app:startDestination="@id/your fragment"
this should solve your problem
I manage to solve this problem.
In my case, I was calling val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
before setContentView(binding.root)
so I change the order of the methods and everything was Ok.
I have resolved this, Actually, i set another layout for this activity in setcontentView, When I set the original layout it worked.
So for me it was that inside my navigation graph file the id I declared on
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_id"
app:startDestination="@id/container">
....
Was not the same name as the menu item
<item
android:id="@+id/my_idd"
android:enabled="true"
android:icon="@drawable/icon"
android:title="@string/test_title" />
Changing the id to be the same for both solved the problem
can you try with this?
val selectedFragment: NavHostFragment? = fragmentManager.findFragmentByTag(newlySelectedItemTag) as NavHostFragment
as?
–
Skep NavHostFragment
type –
Skep © 2022 - 2024 — McMap. All rights reserved.
as?
operator. – Skep