Firebase动态链接打开应用后,Fragment自动跳转到导航图的起始目的地Fragment。

3

我正在使用 Firebase 动态链接与另一个用户分享特定产品。我能够创建产品分享链接,该链接生成良好。我将检测到该链接在我的主登录活动中,该活动是应用程序的启动器活动。
该链接也被很好地检测到了。我有两个导航图,一个用于登录流程,另一个用于主应用程序。我需要从登录图中打开主图中的特定片段。我在登录图中检测到该链接并创建一个深度链接,如下所示:

navController.createDeepLink()
.setComponentName(MainActivity::class.java)
.setGraph(R.navigation.nav_graph_main)
.setDestination(R.id.storeFragment)
.setArguments(bundle)
.createPendingIntent()
.send()

问题是当我打开storeFragment时,应用程序会跳转到nav_graph_main的home fragment,而不是停留在newFragment。只有短暂的停留。

我的nav_graph_main如下:

<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/nav_graph_main"
    app:startDestination="@id/homeFragment">

...other fragments

 <fragment
        android:id="@+id/storeFragment"
        android:name="in.indiahaat.feature.main.home.store_screen.StoreFragment"
        android:label="fragment_store"
        tools:layout="@layout/fragment_store" >
        <action
            android:id="@+id/action_storeFragment_pop"
            app:popUpTo="@id/storeFragment"
            app:popUpToInclusive="true" />

        <action
            android:id="@+id/storeFragment_to_reportRetailerFragment"
            app:popUpTo="@id/reportRetailerFragment"
            app:destination="@id/reportRetailerFragment" />
    </fragment>

...other fragments

</navigation>


请发布一个解决方案,因为我已经被卡在这个问题上一段时间了。

我有两个导航图。一个用于登录过程,另一个用于主应用程序。根据文档和《导航导航》视频的说明,这不是使用导航组件进行登录的正确方式,特别是由于深度链接的工作方式。 - ianhanniballake
1个回答

1
我有一种类似的重定向方式,根据条件重定向到不同的片段。但情况不同。
我必须根据已登录用户的类型和注册状态重定向到不同的片段。使用子导航也可以实现相同的效果。
为此,我需要将数据/条件发送到着陆片段。基于这些数据,可以进行重定向。
您的情况也可以尝试类似的方法。
注意:下面的内容没有包括Firebase和相应的依赖项。
<!-- Parent Graph -->
<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/nav_graph"
    app:startDestination="@id/landingFragment">


       <!-- Landing Child Graph -->
       <navigation
        android:id="@+id/landingFragment"
        app:startDestination="@id/dashboardFragment">
        <fragment
            android:id="@+id/dashboardFragment"
            android:name="com.....DashboardFrag"
            tools:layout="@layout/fg_dashboard">

             <argument
                android:name="CONTINUE_REGISTRATION"
                android:defaultValue="false"
                app:argType="boolean" />

             <action
                android:id="@+id/continueRegistration"
                app:destination="@id/registration_graph" />

        </fragment>

        </navigation>

       <!-- Child Graph -->
       <navigation
        android:id="@+id/registration_graph"
        app:startDestination="@id/opRegFragment">

         <fragment
            android:id="@+id/opRegFragment"
            android:name="com.....RegFieldsFragment"
            tools:layout="@layout/fg_registration">

        </fragment>

       </navigation>
</navigation>

现在在仪表盘碎片中,请按以下方式进行操作。
class DashboardFrag : BaseFrag {

     private var continueReg: Boolean = false

      override fun onCreate(savedInstanceState: Bundle?) {
        DashboardFragArgs.fromBundle(requireArguments()).apply {
            continueReg = CONTINUEREGISTRATION
        }
        super.onCreate(savedInstanceState)
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

       if (continueReg) {
         registrationContinueAction()
       }

    }
  private fun registrationContinueAction() {

       val action = DashboardFragDirections.continueRegistration()
       findNavController().navigate(action)
  }
}

编辑 1:

根据您的评论,您也可以将打开新片段作为单独图形的一部分移动。使用以下代码应该可以实现:

主要 nav_graph.xml

<!-- Parent Graph -->
    <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/nav_graph"
        app:startDestination="@id/landingFragment">
    
    
           <!-- Landing Child Graph -->
           <navigation
            android:id="@+id/landingFragment"
            app:startDestination="@id/dashboardFragment">
            <fragment
                android:id="@+id/dashboardFragment"
                android:name="com.....DashboardFrag"
                tools:layout="@layout/fg_dashboard">
    
                 <argument
                    android:name="CONTINUE_REGISTRATION"
                    android:defaultValue="false"
                    app:argType="boolean" />
    
                 <action
                    android:id="@+id/continueRegistration"
                    app:destination="@id/registration_graph" />
    
            </fragment>

            </navigation>
    </navigation>

现在在新图表中(sub_nav_graph.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"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/sub_nav_graph"
            app:startDestination="@id/SubGraphLangingFg">


<fragment android:id="@+id/SubGraphLangingFg"
              android:name="com....SubGraphFirstFg"
              android:label="fragment_landingsub_fg"
              tools:layout="@layout/fragment_landingsub_fg">

    </fragment>

</navigation>

现在,最好创建一个新的Activity来处理此流程下的所有子片段。这取决于需求和可行性。

在Manifest.xml中注册新的Activity。

<activity android:name="com.....ChildGraphLandingActivity" />

And the new Activity would be:

class ChildGraphLandingActivity : AppCompatActivity() {

       override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_subgraph)

    }

}

activity_subgraph.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.....ChildGraphLandingActivity">


    <fragment
            android:id="@+id/fragment_container"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/sub_nav_graph" />
    
</FrameLayout>

现在在调用的片段中,需要完成以下操作。
class DashboardFrag : BaseFrag {

......

private fun registrationContinueAction() {
       //If you are expecting a call back
       startActivityForResult(Intent(context, ChildGraphLandingActivity::class.java)....)

      //If call back is not expected
      startActivity(Intent(context, ChildGraphLandingActivity::class.java))
  }

}

你的两个图表在同一个导航文件中。在我的情况下,这些图表是分开的文件。我尝试过使用<include />,但它也产生了同样的结果。 - Kavach Chandra
如果图形是分开的,上述编辑将起作用。我正在使用和解释相同的方式。 - Sreehari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接