带有深层链接的Android导航组件:onNewIntent多次调用 [英] Android navigation components with deep link: onNewIntent called multiple times

本文介绍了带有深层链接的Android导航组件:onNewIntent多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一次,我需要您在使用带有深层链接的android导航组件方面提供帮助。

This time I need your help regarding the use of android navigation components with deeplink.

我一直在关注文档,并且fragment和deeplink之间的连接正常。

I have been following this documentation and the connection between fragment and deeplink is working fine.

问题是关于正在接受深度链接的活动。就我而言,我设置了android:launchMode = singleTask

The problem comes in regards to the activity that is receiving the deeplink. In my case, I set the android:launchMode="singleTask"

<activity android:name=".features.welcome.WelcomeActivity"
    android:launchMode="singleTask">
     <nav-graph android:value="@navigation/welcome_nav_graph" />
</activity>

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    Timber.d("onNewIntent: $intent with activity: $this")
    navController.handleDeepLink(intent)
}

通过此配置,我注意到了几个奇怪的行为:

With this configuration I noticed a couple of weird behaviours:

每次我点击深层链接,WelcomeActivity都会收到两次onNewIntent调用。有时甚至创建了该活动的新实例。.例如

WelcomeActivity receives onNewIntent call two times every time I click the deeplink. Having even sometimes new instances of that activity created.. like

1_ object1-onNewIntent

1_ object1-onNewIntent

2_ object1-onNewIntent

2_ object1-onNewIntent

3_ object2-onCreate

3_ object2-onCreate

此处有一些日志:


首次启动

First launch

onCreate:意图{flg = 0x10000000
cmp = {applicationId} / {package}。带有活动的WelcomeActivity}:
{package}。WelcomeActivity@ 4adbef0

onCreate: Intent { flg=0x10000000 cmp={applicationId}/{package}.WelcomeActivity } with activity: {package}.WelcomeActivity@4adbef0

打开深层链接

onNewIntent :意向{act = android.intent.action.VIEW
cat = [android.intent.category.BROWSABLE] dat = https:// {depp_link}…
flg = 0x10010000 cmp = {applicationId} / {package} .WelcomeActivity(具有
个附加项目)},活动:{package} .WelcomeActivity @ 4adbef0

onNewIntent: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}… flg=0x10010000 cmp={applicationId}/{package}.WelcomeActivity (has extras) } with activity: {package}.WelcomeActivity@4adbef0

onNewIntent:意图{act = android.intent.action .VIEW
cat = [android.intent.category.BROWSABLE] dat = https:// {depp_link} ...
flg = 0x1001c000 cmp = {applicationId} / {package} .WelcomeActivity(具有
活动):{package} .WelcomeActivity @ 4adbef0

onNewIntent: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}... flg=0x1001c000 cmp={applicationId}/{package}.WelcomeActivity (has extras) } with activity: {package}.WelcomeActivity@4adbef0

onCreate:Intent {act = android.intent.action.VIEW
cat = [android。 intent.category.BROWSABLE] dat = https:// {depp_link} ...
flg = 0x1001c000 cmp = {applicationId} / {package}。WelcomeActivity(具有
个附加项)},活动:{package } .WelcomeActivity @ b77c6b

onCreate: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}... flg=0x1001c000 cmp={applicationId}/{package}.WelcomeActivity (has extras) } with activity: {package}.WelcomeActivity@b77c6b

杀死该应用并打开深层链接

Kill the app and open deep link

onCreate:意图{act = android。 intent.action.VIEW
cat = [android.intent.category.BROWSABLE] dat = https:// {depp_link} ...
flg = 0x10018000 cmp = {applicationId} / {package} .WelcomeActivity (具有
个附加项)},活动:{package} .WelcomeActivity @ b78f4df

onCreate: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}... flg=0x10018000 cmp={applicationId}/{package}.WelcomeActivity (has extras) } with activity: {package}.WelcomeActivity@b78f4df

onNewIntent:Intent {act = android.intent.action.VIEW
cat = [android.intent.category.BROWSABLE] dat = https:// {depp_link} ...
flg = 0x1001c000 cmp = {applicationId} / {package}。WelcomeActivity(有
个附加项) },活动:{package} .WelcomeActivity @ b78f4df

onNewIntent: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}... flg=0x1001c000 cmp={applicationId}/{package}.WelcomeActivity (has extras) } with activity: {package}.WelcomeActivity@b78f4df

onC reate:意向{act = android.intent.action.VIEW
cat = [android.intent.category.BROWSABLE] dat = https:// {depp_link} ...
flg = 0x1001c000 cmp = { applicationId} / {package} .WelcomeActivity(具有
个附加项)}与{package} .WelcomeActivity @ dfe87b2

onCreate: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://{depp_link}... flg=0x1001c000 cmp={applicationId}/{package}.WelcomeActivity (has extras) } with {package}.WelcomeActivity@dfe87b2

更新:

1-似乎启动模式与此问题无关。我注意到默认启动模式也是如此。

1 -It seems launch mode has nothing to with this issue. I noticed the same with default launch mode.

2- navController.navigate(intent.dataString.toUri())似乎可以正常工作。所以我想问题出在navController.handleDeepLink(intent)。

2- navController.navigate(intent.dataString.toUri()) seems to work fine. So I guess the problem is navController.handleDeepLink(intent).

推荐答案

这听起来很奇怪,但实际上似乎可以正常工作。 有关隐式深层链接的文档说:

It feels weird, but it actually seems to work as intended. The documentation regarding the implicit deep links says:


触发隐式深层链接时,后退堆栈的状态取决于隐式Intent是否使用Intent启动。FLAG_ACTIVITY_NEW_TASK标志:

When triggering an implicit deep link, the state of the back stack depends on whether the implicit Intent was launched with the Intent.FLAG_ACTIVITY_NEW_TASK flag:

如果设置了该标志,则清除任务后退堆栈并替换为深度链接目标...。

If the flag is set, the task back stack is cleared and replaced with the deep link destination....

如果未设置该标志,则将保留在先前应用程序的任务堆栈上,在该任务堆栈上触发了隐式深层链接。

If the flag is not set, you remain on the task stack of the previous app where the implicit deep link was triggered.

在您的情况下,我相信当您点击链接时,该意图带有标志 Intent.FLAG_ACTIVITY_NEW_TASK 设置,因此将创建整个新堆栈。当前实现只是使用新创建的堆栈重新启动活动,以确保任务状态一致。

In your case I believe when you tap a link the intent has the flag Intent.FLAG_ACTIVITY_NEW_TASK set, so the whole new stack is created. Currently the implementation just restarts the activity with the newly created stack to make sure the task state is consistent.

如果不需要这种行为,一种可能的解决方法是在导航组件有机会处理之前,从意图中清除标志 Intent.FLAG_ACTIVITY_NEW_TASK

If you don't need this behavior, one possible workaround would be to clear the flag Intent.FLAG_ACTIVITY_NEW_TASK from the intent before navigation component has a chance to handle it.

这篇关于带有深层链接的Android导航组件:onNewIntent多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆