导航组件:从Viewpager中的片段导航到另一个片段

问题描述

我有一个Fragment A可以导航到Fragment B

Fragment B托管一个ViewPager2

ViewPager2中有两个片段-Fragment CFragment D

在单击Fragment C中的项目时,我想导航到Fragment D上方的Fragment B的新实例。

目前,我的代码是这样的-

findNavController().navigate(
            R.id.action_FragmentC_to_FragmentD,bundleOf("id" to id,"type" to "list")
        )

但是,当我尝试导航时,出现以下异常-

java.lang.IllegalArgumentException: Navigation action/destination com.test.at:id/action_FragmentC_to_FragmentD cannot be found from the current destination Destination(com.krtkush.audiotime:id/FragmentB) label=FragmentB

据我了解,导航组件仍在Fragment B上,但是我试图从Fragment D导航到Fragment C,因此引发异常。

我(认为)可以通过添加一个侦听器来解决此问题,该侦听器会在Fragment B中敲击项目时通知Fragment C,然后从Fragment D导航至Fragment B。但是,这会使我的Fragment CFragment B紧密耦合,而我不想这么做。反正要解决这个问题是另一种方式吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)