16

In my project, I use the navigationextensions class "with 5 different navigation graph" from google example to handle the bottom navigation bar navigation in my project.

goolge exmaple : https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample.

I would like to navigate to fragment A in more than one graph.

"Now I get this error : navigation destination action_compareListFragment_to_productPageFragment is unknown to this NavController"

logcat :

2019-11-26 10:34:02.502 6094-6094/com..app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com..app, PID: 6094
    java.lang.IllegalArgumentException: navigation destination com..app:id/action_compareListFragment_to_productPageFragment is unknown to this NavController
        at androidx.navigation.NavController.navigate(NavController.java:863)
        at androidx.navigation.NavController.navigate(NavController.java:804)
        at androidx.navigation.NavController.navigate(NavController.java:790)
        at com..app.views.home.accountPage.FavoriteFragment.onGoToProductClicked(FavoriteFragment.java:68)
        at com..app.databinding.FavoriteListItemBindingImpl._internalCallbackOnClick(FavoriteListItemBindingImpl.java:365)
        at com..app.generated.callback.OnClickListener.onClick(OnClickListener.java:11)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

How should I handle this situation?

I would appreciate if anyone helped me .

motaz lubbad
  • 181
  • 1
  • 5

1 Answers1

15

The fragment that you want to use in multiple navigation graphs should have the same id in each navigation graph. Also, in each graph, the same fragment should have the same fragment arguments and the same actions with same ids. If you fail to meet these conditions the Navigation Component won't generate the code at all and build fails.

Tartar
  • 5,149
  • 16
  • 63
  • 104
  • is there any alternative way to implement, Use the same fragment with different actions? – Arul Nov 20 '20 at 08:28
  • 6
    Create a nested graph for that fragment and use it at every place. – Nilesh Tiwari Dec 17 '20 at 14:37
  • At first, thanks @Tartar for answer it helps me, but... I tested it. The same fragment should have the same fragment arguments, but not the same id or actions list. – maatik5 Mar 22 '22 at 22:49
  • 1
    Is there a source/reference to this? – hopia Aug 05 '22 at 01:58
  • No, I just found it by trying at it worked by the time I tried. Considering that the library gets updated, the behaviour might have changed. – Tartar Feb 15 '23 at 15:49