Reputation: 1029
I am having an issue with the Android Navigation Architecture component when I try to navigate from one Fragment to another within a viewpager, I get this error:
java.lang.IllegalArgumentException: Navigation action/destination
com.gigaweb.mysales:id/action_mainFragment_to_addTransactionFragment cannot be found from the current
destination Destination(com.gigaweb.mysales:id/pagerFragment) label=fragment_pager class=com.gigaweb.mysales.PagerFragment
I have a viewpager which I use to navigate between two Fragments and it works just fine. the problem is that I have a button within one of the fragments, the button is also used to navigate to another Fragment using navcontroller, and when the button is clicked the app crashes and I get the error above.
Update This is the navGraph
As you can see the SignIn Fragment is the start Destination and there is an action liking it to the pagerFragment which serves as the host for the ViewPager
the way I want the app to work is:
When the app launches The signIn Fragment is shown ..... working
Navigate to PagerFragment when SignIn Button is clicked ..... working
Navigate between MainFragment and AdminFragment by swiping left or right ..... working
Navigate to AddTransactionFragment whin the FAB Button is clicked ..... NOT WORKING!! as the button is clicked the app crashes.
Update Here is the XML code for the Navigation Graph
<?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/signInFragment">
<fragment
android:id="@+id/signInFragment"
android:name="com.gigaweb.mysales.SignInFragment"
android:label="fragment_sign_in"
tools:layout="@layout/fragment_sign_in" >
<action
android:id="@+id/action_signInFragment_to_pagerFragment"
app:destination="@id/pagerFragment" />
</fragment>
<fragment
android:id="@+id/addTransactionFragment"
android:name="com.gigaweb.mysales.AddTransactionFragment"
android:label="AddTransactionFragment" >
<action
android:id="@+id/action_addTransactionFragment_to_mainFragment"
app:destination="@id/mainFragment" />
</fragment>
<fragment
android:id="@+id/mainFragment"
android:name="com.gigaweb.mysales.MainFragment"
android:label="MainFragment" >
<action
android:id="@+id/action_mainFragment_to_addTransactionFragment"
app:destination="@id/addTransactionFragment" />
</fragment>
<fragment
android:id="@+id/adminFragment"
android:name="com.gigaweb.mysales.AdminFragment"
android:label="AdminFragment" />
<fragment
android:id="@+id/pagerFragment"
android:name="com.gigaweb.mysales.PagerFragment"
android:label="fragment_pager"
tools:layout="@layout/fragment_pager" />
</navigation>
Upvotes: 4
Views: 9372
Reputation: 6956
For my case, I resolve the problem by replacing -
<action
android:id="@+id/action_mainFragment_to_addTransactionFragment"
app:destination="@id/addTransactionFragment" />
with
<action
android:id="@+id/action_mainFragment_to_addTransactionFragment"
app:popUpToInclusive="true" /* If true then also remove the destination from stack while popup */
app:popUpTo="@id/mainFragment" /*The fragment where to land again from destination*/
app:destination="@id/addTransactionFragment" />
Upvotes: 0
Reputation: 312
For my case, it turns out it caused by calling dismissAllowingStateLoss();
after navigating to new destination.
Upvotes: 0
Reputation: 200100
If you do not navigate()
to MainFragment
and AdminFragment
, you will remain on the last destination you navigated to: PagerFragment
, that is the expected behavior. The NavController knows nothing about child fragments such as fragments within a ViewPager of PagerFragment
and therefore you were never on MainFragment
as a destination.
If you never navigate()
to MainFragment
or AdminFragment
and they are only viewable as part of your ViewPager, then MainFragment
and AdminFragment
should not be in your graph. Any actions from fragments within PagerFragment
's ViewPager should be actions on PagerFragment
directly (the destination you're actually on).
Upvotes: 9