我试图使用带有嵌套图形的安全Args来使用全球行动,但我得到了一个错误。
FragmentA.kt:
class FragmentA : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_a, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btFragmentB.setOnClickListener {
NavHostFragment.findNavController(this)
.navigate(FragmentBDirections.actionGlobalFragmentB())
}
}
}
FragmentB.kt:
class FragmentB : Fragment()
main.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_nav_graph"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.cookpad.arcshowcase.stack.FragmentA"
android:label="FragmentA" />
<include app:graph="@navigation/fragment_b" />
</navigation>
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_b"
app:startDestination="@id/fragmentB">
<fragment
android:id="@+id/fragmentB"
android:name="com.cookpad.arcshowcase.stack.FragmentB"
android:label="FragmentB" />
<action
android:id="@+id/action_global_fragmentB"
app:destination="@id/fragmentB" />
</navigation>
当我运行这个程序时,我得到了一个错误:java.lang.IllegalArgumentException: navigation destination com.cookpad.arc:id/action_global_fragmentB is unknown to this NavController
,它似乎表明action_global_fragmentB从未从子图中合并。
有没有一种方法可以使用从外层图中在嵌套图中声明的全局操作?
发布于 2022-11-20 09:43:55
您只需要将全局操作移动到主图形导航。
main.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_nav_graph"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.cookpad.arcshowcase.stack.FragmentA"
android:label="FragmentA" />
<include app:graph="@navigation/fragment_b" />
<action
android:id="@+id/action_global_fragmentB"
app:destination="@id/fragment_b" />
</navigation>
fragment_b.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_b"
app:startDestination="@id/fragmentB">
<fragment
android:id="@+id/fragmentB"
android:name="com.cookpad.arcshowcase.stack.FragmentB"
android:label="FragmentB" />
</navigation>
FragmentA.kt
class FragmentA : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_a, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btFragmentB.setOnClickListener {
NavHostFragment.findNavController(this)
.navigate(FragmentADirections.actionGlobalFragmentB())
}
}
}
https://stackoverflow.com/questions/61322962
复制相似问题