ISSUE
java.lang.IllegalArgumentException: Navigation action/destination xxxx/action_scanFragment_to_addVehicleFragment cannot be found from the current destination xxxx/addVehicleFragment
The error occurs when I do
findNavController().navigate(R.id.action_scanFragment_to_addVehicleFragment)
in scanFragment. Which means that current destination is addVehicleFragment, but it should be scanFragment.
I am clueless on how to approach this. See my earlier question for some troubleshooting and what really goes on in scanFragment: Kotlin - fragment lifecycle navigation issues; why does child fragment become current destination?
I suspect my navigation setup is wrong, but I can't find the solution anywhere.
I'm posting my entire navigation implementation/code below.
My nav_graph design:

My nav_graph XML:
<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/scanFragment"
android:name="xxxx.ui.scan.ScanFragment"
android:label="@string/tab_bar_first_item_title"
tools:layout="@layout/fragment_scan" >
<action
android:id="@+id/action_scanFragment_to_addVehicleFragment"
app:destination="@id/addVehicleFragment"
app:enterAnim="@anim/from_left"
app:exitAnim="@anim/to_left" />
</fragment>
<fragment
android:id="@+id/addVehicleFragment"
android:name="xxxx.ui.scan.AddVehicleFragment"
android:label="@string/add_vehicle_fragment_title_string"
tools:layout="@layout/fragment_add_vehicle">
<action
android:id="@+id/action_addVehicleFragment_to_scanFragment"
app:destination="@id/scanFragment" />
</fragment>
<fragment
android:id="@+id/mainFragment"
android:name="xxxx.ui.main.MainFragment"
android:label="@string/tab_bar_second_item_title"
tools:layout="@layout/fragment_main" />
<fragment
android:id="@+id/profileFragment"
android:name="xxxx.ui.profile.ProfileFragment"
android:label="@string/tab_bar_third_item_title"
tools:layout="@layout/fragment_profile">
<action
android:id="@+id/actionMyVehicles"
app:destination="@id/myVehiclesFragment"
app:enterAnim="@anim/from_right"
app:exitAnim="@anim/to_left" />
<action
android:id="@+id/actionMyRooms"
app:destination="@+id/myRoomsFragment"
app:enterAnim="@anim/from_right"
app:exitAnim="@anim/to_left" />
</fragment>
<fragment
android:id="@+id/myVehiclesFragment"
android:name="xxxx.ui.profile.MyVehiclesFragment"
android:label="fragment_my_vehicles"
tools:layout="@layout/fragment_my_vehicles" >
<action
android:id="@+id/action_myVehiclesFragment_to_profileFragment"
app:destination="@id/profileFragment"
app:enterAnim="@anim/from_left"
app:exitAnim="@anim/to_right" />
</fragment>
<fragment
android:id="@+id/myRoomsFragment"
android:name="xxxx.ui.profile.MyRoomsFragment"
android:label="fragment_my_rooms"
tools:layout="@layout/fragment_my_rooms" >
<action
android:id="@+id/action_myRoomsFragment_to_profileFragment"
app:destination="@id/profileFragment"
app:enterAnim="@anim/from_left"
app:exitAnim="@anim/to_right" />
</fragment>
</navigation>
My Main Activity XML:
<?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:id="@+id/main_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_gray"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/tabBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph">
</androidx.fragment.app.FragmentContainerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/tabBar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_alignParentBottom="true"
app:elevation="2dp"
app:itemBackground="@color/light_gray"
app:itemIconSize="30dp"
app:itemIconTint="@color/tab_bar_icon_tint_color"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/tab_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
class MainActivity : AppCompatActivity() {
private lateinit var viewBinding: ActivityMainBinding
private lateinit var tabBar: BottomNavigationView
private lateinit var navHostFragment: NavHostFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
println("MainActivity || onCreate ||")
// Initialize viewBinding object
viewBinding = ActivityMainBinding.inflate(layoutInflater)
// Hide the default navigation bar; we implement our own
supportActionBar!!.hide()
viewBinding.loadingPageLayout.visibility = View.GONE
setContentView(viewBinding.root)
setupBottomBarNavigation()
}
private fun setupBottomBarNavigation() {
tabBar = viewBinding.tabBar
navHostFragment = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
val navigationController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(setOf(R.id.scanFragment, R.id.mainFragment, R.id.profileFragment))
setupActionBarWithNavController(navigationController, appBarConfiguration)
tabBar.setupWithNavController(navigationController)
val navHost = supportFragmentManager.currentNavigationFragment
println("Current navigation fragment: $navHost")
}
override fun onSupportNavigateUp() =
Navigation.findNavController(this, R.id.navHostFragment).navigateUp()
Please help a desperate fellow out!
If someone had the same issues due to multiple clicks on the screen. It can be resolved by checking the current destination first before navigating
For example
Fragments A, B, and C
navigating from A to B while clicking on a button in fragment A that navigates to C might lead to crashes in some cases
for that you should check the current destination first as follows:
if(findNavController().currentDestination?.id==R.id.AFragment)
findNavController().navigate(
AFragmentDirections.actionAFragmentToCFragment()
)
The problem was not in Navigation Components but instead - I think - in my ImageAnalyzer. Turns out I accidentally allowed multiple calls to findNavController.navigate(). Now that I fixed that, my navigation issue is gone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With