android - 如何让我的 flutter 应用程序意识到它是以不同的 Intent 启动的?

标签 android kotlin flutter

我有一个 Flutter 应用程序,它可以像浏览器一样从启动器或通过 URI Intent 启动。

我有两个文件。包含主要代码的 Dart 文件和包含 Intent 捕获代码的 Kotlin 文件。

我的 Kotlin 代码如下所示:

class MainActivity : FlutterActivity() {
    override fun onResume() {
        super.onResume()
        print("got at resume ${intent.data}")
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        print("At onCreate: ${intent.data}")
        GeneratedPluginRegistrant.registerWith(this)
        MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
            print("got at onCall ${intent.data}")
    }
}

所以当我第一次启动它时,它输出 At onCreate: null"。有道理,我无意中启动了它。

然后我运行 adb shell am start -d "https://www.example.com"。我希望它(当我编写一个演示 Android 应用程序时,它确实)输出 print("At onCreate: https://www.example.com")

但是,当我将它作为 Dart 应用程序运行时,我得到 FlutterActivityDelegate: onResume setting current activity to this。即使我将它发送到后台然后运行 ​​adb shell...,我也会得到 got at resume null

我获得 At onCreate: https://www.example.com 的唯一方法是将其从之前的应用列表中删除。然后,直到那时我才得到正确的输出。

但即便如此,运行 adb shell am start -d "https://www.example.com/123" 也会产生与上述相同的错误。

如何在 Dart/Kotlin 中获得"new" Intent ?

最佳答案

在您的 AndroidManifest.xml 中,android:launchMode 是相关的。如果它设置为 singleTopActivity 将收到对 onNewIntent(Intent) 的调用:

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

关于android - 如何让我的 flutter 应用程序意识到它是以不同的 Intent 启动的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49291697/

相关文章:

java - 保存对话框输入

flutter - 如何触发任何其他有状态类的 setState()?

swift - Kotlin 中有没有办法让接口(interface)拥有关联的类型契约?

spring - 无法使用 IntelliJ 和 docker 调试 Kotlin Tomcat Web 应用程序

android-studio - Android Studio 模拟器使 Windows 10 崩溃

android - “未找到 Android 属性。”构建 Flutter aar 时出错

android - 我想使用 mediaplayer 来录制声音,然后计算 "amplitude"以随着声音的变化上下移动 Bars

android - 如何使用 JUnit 测试显示 Android 日志记录输出(使用不带模拟器的 native JUnit)

java - 如何计算自记录输入以来的天数?安卓、SQLite、Java

Android 存储库模式将 room 转换为域模型