architecture-components-samples
architecture-components-samples copied to clipboard
[NavigationAdvancedSample] How to use root graph and global action?
I am trying to extend the [NavigationAdvancedSample] repo with the global action/destination like this: `app_navigation.xml <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/app_navigation" app:startDestination="@id/home">
<include app:graph="@navigation/home" />
<include app:graph="@navigation/list" />
<include app:graph="@navigation/form" />
<action android:id="@+id/action_open_detail"
app:destination="@id/nav_detail"/>
<fragment android:id="@+id/nav_detail"
android:name="com.example.android.navigationadvancedsample.detail.Detail"
android:label="Detail"
tools:layout="@layout/fragment_detail"/>
`
I'm looking for the solution to navigate to nav_detail by action action_open_detail from home/list or form. But It's doesn't working. Have any suggest to use this functions like up with [NavigationAdvancedSample]?
What we did in our project would look like this:
Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
.navigate(R.id.action_open_detail)
basically you need to get the top NavController. There is no way of getting to nav_detail from tab graphs afaik
@ildar2 Can you share your nav graphs please?
I got this exception while try to open detail page from a sub graph
java.lang.IllegalArgumentException: Navigation action/destination id/action_open_detail cannot be found from the current destination Destination
@yusufceylan
<navigation1 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/mobile_navigation"
app:startDestination="@+id/main">
<action
android:id="@+id/action_auth"
app:destination="@id/auth_navigation" />
<action
android:id="@+id/action_rename"
app:destination="@id/tab_products" />
<fragment
android:id="@+id/main"
android:name="ui.main.MainFragment" <-- this fragment contains bottom tab graphs -->
tools:layout="@layout/fragment_main" />
<dialog
android:id="@+id/auth_needed"
android:name="ui.auth.AuthNeededDialog"
tools:layout="@layout/include_auth_needed" />
<dialog
android:id="@+id/share_schedule"
android:name="ui.main.products.detail.ShareDialog"
tools:layout="@layout/dialog_credit_schedule_share" />
<include app:graph="@navigation/auth_navigation" />
</navigation>
Thanks @ildar2 , it works with this approach because beside Main Fragment have sub nav graphs, it also has it's own nav graph. But I think it is not possible to create a global destination with @sinhpn92 's approach
@yusufceylan That's to make include more usefull with me to reuse action and fragment from global destination instead of define on each navigation. It's duplicate code. :((