I have very simple navigation and when I attempt to go to new fragment I get this error:
java.lang.IllegalArgumentException: Navigation action/destination com.my.app:id/action_settingsFragment_to_profileFragment cannot be found from the current destination Destination(com.my.app:id/mainFragment) label=fragment_main class=com.my.app.main.MainFragment
navigation
<?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/main_graphs"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.my.app.main.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/action_mainFragment_to_laundriesFragment"
app:destination="@id/laundriesFragment" />
</fragment>
<!-- other fragments here... -->
<fragment
android:id="@+id/settingsFragment"
android:name="com.my.app.main.SettingsFragment"
android:label="fragment_settings"
tools:layout="@layout/fragment_settings" >
<action
android:id="@+id/action_settingsFragment_to_newOrderFragment"
app:destination="@id/newOrderFragment" />
<action
android:id="@+id/action_settingsFragment_to_profileFragment"
app:destination="@id/profileFragment" />
</fragment>
<fragment
android:id="@+id/profileFragment"
android:name="com.my.app.main.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
</fragment>
</navigation>
SettingsFragment.kt
(redirect code)
when (position) {
0 -> {
//....
}
1 -> {
navController.navigate(R.id.action_settingsFragment_to_profileFragment)
}
2 -> {
//...
}
}
Any idea?
Update
I've managed to redirect to profileFragment
with following code, but when I hit back button it closes the app instead of back to settingsFragment
private fun GenerateItems(){
when (position) {
0 -> {
//
}
1 -> {
this@SettingsFragment.navigateTo(ProfileFragment())
}
2 -> {
//
}
}
}
private fun navigateTo(fragment: Fragment) {
val transaction = (activity as FragmentActivity).supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentFragmentId, fragment)
transaction.commit()
}
Any idea about back button issue?