通过Firebase动态链接打开应用程序后,打开应用程序时,片段会自动跳至导航图的startdestination片段

问题描述

我正在使用Firebase Dynamic链接与其他用户共享特定产品。我可以创建产品共享链接链接产生良好。我在主要登录活动(该应用的启动程序活动)中检测到链接
链接也被很好地检测到。我有2个导航图。一种用于登录过程,另一种用于主应用程序。我必须从登录图在主图中打开一个特定的片段。我正在检测登录图中的链接并创建一个链接,如下所示:

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

问题在于,当我打开storeFragment时,应用程序跳至nav_graph_main的主片段。它不会停留在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>

请为我发布解决方案,因为我已经坚持了一段时间。

解决方法

我有一种根据条件重定向到不同片段的类似方法。但是情况有所不同。

我必须根据登录用户的类型和注册状态重定向到不同的片段。使用子导航也可以。

为此,我不得不将数据/条件发送到着陆片段。基于此数据重定向是可能的。

对于您的情况,也可以尝试类似的方法。

注意:尽管以下内容不包括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 android:name="com.....ChildGraphLandingActivity" />

新的活动将是:

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))
  }

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...