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.
Ways to reproduce
- create new "Navigation Drawer Activity" project.
- add classpath to dependencies of top level gradle file
def nav_version = "2.3.1"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
- add apply plugin line to module level gradle file
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()
}
}
- add dialog to mobile_navigation.xml and action from home to dialog
<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"
/>
- add click action to onCreateView of HomeFragment
val showDialog: Button = root.findViewById(R.id.showDialog)
showDialog.setOnClickListener { findNavController().navigate(HomeFragmentDirections.actionNavHomeToDialog()) }
- Run app
- tap "show dialog" button again and again
from Navigation safeArgs fails to navigate after back from dialog
No comments:
Post a Comment