我使用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的主片段。它不呆在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>请发布一个解决方案,因为我已经坚持了一段时间了。
发布于 2020-10-06 08:01:58
我有一种类似的方法,可以根据某种情况重定向到不同的片段。但情况不一样。
我必须根据登录用户的类型和注册状态重定向到不同的片段。有了子导航,这是可能的。
为此,我不得不将数据/条件发送到着陆碎片。基于这个数据,重定向是可能的。
对于您的场景,也可以尝试类似的方法。
注意:尽管下面没有包含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>现在,最好有一个新的活动来处理这个流下的所有子片段。这又取决于要求和可行性。
在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))
}
}https://stackoverflow.com/questions/64115956
复制相似问题