I'm getting Illegal argumentException: Navigation action/destination pkgname:id/action_nav_home_to_dialog cannot be found from the current destination Destination(pkgname:id/dialog)
when navigate from HomeFragment to other fragment using safeArgs directions.
Even though Button in HomeFragment clicked,the current destination is pkgname:id/dialog.
should I check if the current destination is HomeFragment when navigate from HomeFragment to other Fragment every time ? should I write some code on the dialog close?
In my investigation, it seems not happen after closing normal fragment.
def nav_version = "2.3.1"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
apply plugin: "androidx.navigation.safeargs.kotlin"
4.create dialog fragment file
package pkgname.ui
import android.app.Dialog
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
class MyDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(requireActivity())
.setMessage("hello").create()
}
}
<fragment
android:id="@+id/nav_home"
android:name="pkgname.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/action_nav_home_to_dialog"
app:destination="@id/dialog" />
</fragment>
<dialog
android:id="@+id/dialog"
android:name="pkgname.ui.MyDialog" />
6.add button to gragment_home.xml
<com.google.android.material.button.MaterialButton
android:id="@+id/showDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show dialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
val showDialog: Button = root.findViewById(R.id.showDialog)
showDialog.setOnClickListener { findNavController().navigate(HomeFragmentDirections.actionNavHomeToDialog()) }
I tried your code. It was working fine. The dialog shows and dismiss as expected until I double clicked on the "SHOW DIALOG" button.The app started to crash when double clicked on the button.
In my case the error shown in the logcat was,
java.lang.IllegalArgumentException: navigation destination com.xxx.navigationdraweractivity:id/action_nav_home_to_dialog is unknown to this NavController
As you asked in your question checking for the current destination solved the problem for me.
Change the code of button click in your HomeFragment like this,
showDialog.setOnClickListener {
if (findNavController().currentDestination?.id == R.id.nav_home){
findNavController().navigate(HomeFragmentDirections.actionNavHomeToDialog())
}
}
The solution is from here https://stackoverflow.com/a/56168225/14698807