当我尝试从一个片段导航到另一个片段时,我遇到了新的Android导航架构组件的问题,我得到了这个奇怪的错误:
java.lang.IllegalArgumentException: navigation destination XXX
is unknown to this NavController
Run Code Online (Sandbox Code Playgroud)
每个其他导航工作正常,除了这个特定的导航
我用:
findNavContoller()
Run Code Online (Sandbox Code Playgroud)
Fragment的扩展功能可以访问navControler.
任何帮助将不胜感激.
我有一个BottomNavigationView和一个应用程序ViewPager.如何使用新的"导航架构组件"实现它?
什么是最佳做法?
非常感谢
对于新的应用程序,我使用Jetpack导航库来实现正确的后退导航.第一级导航是导航抽屉,如文档中所述,可以使用jetpack导航.但是ViewPager和TabLayout实现了另一个导航级别.由TabLayout切换的片段包含额外的线性导航层次结构.但是,在Jetpack Navigation中似乎不支持ViewPager/TabLayout.必须实现FragmentPagerAdapter,并在切换选项卡时结束托管后端堆栈.顶级导航与每个选项卡内的导航之间存在脱节.有没有办法让这个工作与Jetpack导航?
android android-navigation android-architecture-components android-jetpack android-architecture-navigation
这是场景:-
我有一个主活动,它具有根 NavGraph 并默认加载片段 A。如果我从片段 A 移动到片段 B,其中有子片段和 TabLayout,那么用户可以在其中切换片段,为此我为片段 B 内的子片段创建了一个新的嵌套图。当我从片段移动时A 到片段 B ,我能够在我的子片段中显示片段 C,因为我已在嵌套图中将起始目的地设置为片段 C。
***root Navigation Graph***
<?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" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation_graph"
app:startDestination="@id/fragmentC">
<fragment
android:id="@+id/FragmentA"
android:name="com.myapp.fragment.FragmentA"
android:label="Fragment A"
tools:layout="@layout/fragment_A">
<action
android:id="@+id/action_fragmentA_to_fragmentB"
app:destination="@id/fragmentB" />
</fragment>
<fragment
android:id="@+id/fragmentB"
android:name="com.myapp.fragment.FragmentB"
android:label="FragmentB"
tools:layout="@layout/fragment_B">
<action
android:id="@+id/action_fragmentB_to_second_graph"
app:destination="@id/navigation_graph2" />
</fragment>
<include app:graph="@navigation/navigation_graph2" />
</navigation>
***Nested Navigation Graph***
<?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" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation_graph2"
app:startDestination="@id/FragmentC">
<fragment android:id="@+id/FragmentC"
android:name="com.myapp.fragment.FragmentC"
android:label="Fragment C"
tools:layout="@layout/fragment_C">
<action
android:id="@+id/action_fragmentC_fragmentD"
app:destination="@id/FragmentD" />
</fragment>
<fragment android:id="@+id/FragmentD"
android:name="com.myapp.fragment.FragmentD"
android:label="Fragment …Run Code Online (Sandbox Code Playgroud) android android-architecture-components android-jetpack androidx android-jetpack-navigation
首先,一些澄清。在查看了许多其他相关问题之后,让我们从这不是什么开始:
这个问题是关于如何使用导航组件,当Viewpager是组件之一时。
这种情况有 3 个选项卡:TabA、TabB 和 TabC 也有 2 个额外的片段:FragA、FragB
我如何将它们全部连接起来,以便: ViewPagerAdapter 正在处理 TabA、TabB 和 TabC 并且nav_praph 将处理从以下位置移动:
所需的 UI 行为
第一个选项:我能做的最好的工作是在其中一个选项卡(例如 TabA)中定义 FragHost,然后导航到 FragA。问题是导航是在选项卡内完成的。这意味着用户仍然会看到顶部的选项卡,并且仍然可以滑动到其他选项卡。不是我想要的。这是有效的图表,但不是我想要的(红色代表托管片段)

第二个选项:将 fragHost 移动到最顶部,并让它包含 ViewPager,使其占据全屏。然后在 nagGraph 中提到
<navigation
android:id="@+id/nav_graph"
app:startDestination="@id/tab_a"
>
<fragment
android:id="@+id/tab_a"
android:name="com.myapp.home.views.TabA"
android:layout="@layout/tab_a_layout"
>
<action
android:id="@+id/action_tab_a_to_frag_a"
app:destination="@+id/frag_a"
/>
</fragment>
<fragment
android:id="@+id/frag_a"
android:name="com.myapp.FragA"
android:label="details"
android:layout="@layout/frag_a"
/>
Run Code Online (Sandbox Code Playgroud)
这导致了IllegalStateException......at androidx.fragment.app.FragmentStore.addFragment
一些调试,我看到它试图添加两次 TabA。我假设 ViewPagerAdapter 和 Nav Component 都在使用 Fragment 管理器。 …
android android-jetpack android-architecture-navigation android-jetpack-navigation