Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use same fragment multiple times in different navigation graph

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 .

like image 222
motaz lubbad Avatar asked Nov 26 '19 06:11

motaz lubbad


1 Answers

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.

like image 125
Tartar Avatar answered Nov 08 '22 02:11

Tartar