Android:一起使用com.github.appintro.AppIntro和navigation Graph

问题描述 投票:0回答:0

快照
我有一个项目被分成多个模块。
我有一个应用程序应用程序,其中片段和功能被分成不同的模块。
除了 MainActivity,我现在还有 AppIntro 活动。包含 MainActivity 的模块具有对 AppIntroActivity 模块的 Gradle 引用。

任务
从我的一张幻灯片中,我需要导航到特定片段(在 MainActivity 中)。

问题
我无法将 Intent 发送到 MainActivity(因为引用已经相反)。

因此,我的想法是引用一个包含导航图的模块,该导航图知道通往所需片段的路径。

实施

  • 参考“androidx.navigation:navigation-fragment-ktx”以及包含导航图的模块设置 Gradle 文件。 -> 检查

  • 将 FragmentContainerView 添加到 activity_onboarding.xml -> 检查:

     <androidx.fragment.app.FragmentContainerView
         android:id="@+id/nav_host_fragment"
         android:name="androidx.navigation.fragment.NavHostFragment"
         android:layout_width="0dp"
         android:layout_height="0dp"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent"
         app:layout_constraintBottom_toBottomOf="parent"
         app:defaultNavHost="true"
         app:navGraph="@navigation/nav_graph_onboarding" />
    
  • 设置导航图(“nav_graph_onboarding.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/nav_graph.xml"
         app:startDestination="@id/appIntroFragment">
    
         <fragment
             android:id="@+id/appIntroFragment"
             android:name="com.github.appintro.AppIntroFragment"
             android:label="AppIntroFragment" />
    
         <include app:graph="@navigation/nav_graph_login" />
     </navigation>
    

首先,一切看起来都很好。入职布局打开,我可以浏览它。

这个实现的问题是幻灯片中的 onClickListener(在按钮上)不再有反应。如果我删除“androidx.fragment.app.FragmentContainerView”一切正常。

问题: AppIntro 和导航图如何一起使用?
有可能吗,还是我需要一种完全不同的方法?

android kotlin android-fragments navgraph
© www.soinside.com 2019 - 2024. All rights reserved.