Víc*_*tos 10 android android-jetpack android-architecture-navigation
我尝试使用带有嵌套图形的 Safe Args使用全局操作,但出现错误。
片段A.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())
}
}
}
Run Code Online (Sandbox Code Playgroud)
片段B.kt :
class FragmentB : Fragment()
Run Code Online (Sandbox Code Playgroud)
主文件
<?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>
Run Code Online (Sandbox Code Playgroud)
片段_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>
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到这个错误:java.lang.IllegalArgumentException: navigation destination com.cookpad.arc:id/action_global_fragmentB is unknown to this NavController这似乎表明 action_global_fragmentB 从未从子图中合并。
有没有办法使用从外部图中在嵌套图中声明的全局操作?
您只需将全局操作移至主图导航即可
主文件
<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>
Run Code Online (Sandbox Code Playgroud)
片段_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>
Run Code Online (Sandbox Code Playgroud)
片段A.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())
}
}
}
Run Code Online (Sandbox Code Playgroud)