Reputation: 1971
I'm using Jetpack Navigation in my app, I've now came across a situation I don't fully understand.
I have a fragment CouponFragment
which is supposed to be used in multiple places (inside other fragments via static xml tag and inside a bottomSHeetDialog not connected to navGraph). I need this fragment to be able to navigate to a WebviewFragment
which is a destination in the navGraph.
I added the navigation entry for CouponFragment
:
In the most simple case (CouponFragment
inside another navigation-connected fragment) the solution is pretty straight-forward, I added an action to the fragment which contains CouponFragment
, for example:
<fragment
android:id="@+id/navigation_results"
android:name="ResultsFragment"
tools:layout="@layout/fragment_results">
<action
android:id="@+id/action_webview"
app:destination="@id/navigation_webview" />
</fragment>
This works. The real problem comes when I try to navigate from CouponFragment
which is inside a BottomSheetFragmentDialog
. I tried adding an action to the navigation-connected fragment which invokes the dialog:
<fragment
android:id="@+id/navigation_profile"
android:name="ProfileFragment"
android:label="@string/title_profile"
tools:layout="@layout/fragment_profile">
<action
android:id="@+id/action_webview"
app:destination="@id/navigation_webview" />
</fragment>
but that results in this exeception:
java.lang.IllegalArgumentException: Navigation action/destination package.name:id/action_webview cannot be found from the current destination Destination(package.name:id/navigation_webview) class=WebViewFragment
which is strange becuase looks like is searching the action on the destination fragment. I tried adding a action_webview
to navigation_webview
:
just to see what would happen and nothing does. No logs are printed.
Could anyone give a hint on how to approach this problem?
Upvotes: 1
Views: 953
Reputation: 675
I'll start to unswer to the main question only because the details is very ambiguous and edit if necessary.
To navigate from a non_navigation fragment:
Use a deep link, here is the doc: https://developer.android.com/guide/navigation/navigation-deep-link
Just use the ParentFragmentDirections and the current NavController.
So for example:
to navigate from FragmentY to FragmentB just load the NavController from the MainNavigationHostFragment and set as destination "FragmentADirections.actionFragmentA_to_FragmentB()"
Upvotes: 1