【问题标题】:Implicit deep link does not work properly- onCreate called twice隐式深层链接无法正常工作 - onCreate 调用了两次
【发布时间】:2019-11-19 05:51:35
【问题描述】:

当我从链接打开我的应用程序时,如果它已经在堆栈上(打开),没有任何反应。应用程序的现有实例打开,没有任何重定向。我发现这个意图的原因被称为。所以,我在 onNewIntent 方法中添加了 setIntent(newIntent) ,但它没有帮助。然后我添加了以下代码:

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        //did not help
        //setIntent(intent)
        findNavController(R.id.container).handleDeepLink(intent)
    }

但是,onNewIntent 被调用了两次。点击链接后的日志结果为:(存在应用实例时)

2019-11-19 10:43:18.758 D/CORN_DEBUG: onNewIntent
2019-11-19 10:43:18.805 D/CORN_DEBUG: onNewIntent
2019-11-19 10:43:19.248 D/CORN_DEBUG: onCreate Main Activity

否则,结果为:

2019-11-19 10:45:05.272 D/CORN_DEBUG: onCreate Main Activity
2019-11-19 10:45:05.524 D/CORN_DEBUG: onNewIntent
2019-11-19 10:45:06.041 D/CORN_DEBUG: onCreate Main Activity

当 onCreate 被调用两次时,屏幕会闪烁。有什么问题?

附:启动模式为单任务。

【问题讨论】:

    标签: android kotlin android-architecture-navigation android-deep-link


    【解决方案1】:

    处理深层链接 强烈建议在使用 Navigation 时始终使用默认的标准启动模式。使用标准启动模式时,Navigation 通过调用 handleDeepLink() 自动处理深层链接,以处理 Intent 中的任何显式或隐式深层链接。但是,如果在使用诸如 singleTop 之类的备用启动模式时重新使用 Activity,这不会自动发生。这种情况下,需要在onNewIntent()中手动调用handleDeepLink(),如下例所示:

    https://developer.android.com/guide/navigation/navigation-deep-link#handle

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 2022-10-19
      相关资源
      最近更新 更多