首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用嵌套图中从外部图声明的全局操作?

如何使用嵌套图中从外部图声明的全局操作?
EN

Stack Overflow用户
提问于 2020-04-20 12:51:34
回答 1查看 1K关注 0票数 14

我试图使用带有嵌套图形的安全Args来使用全球行动,但我得到了一个错误。

FragmentA.kt:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
class FragmentB : Fragment()

main.xml

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
<?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从未从子图中合并。

有没有一种方法可以使用从外层图中在嵌套图中声明的全局操作?

EN

Stack Overflow用户

发布于 2022-11-20 09:43:55

您只需要将全局操作移动到主图形导航。

main.xml

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
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())
        }
    }
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61322962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档