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

How to get my flutter app to realize that it was started with a different intent?

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

我有两个文件。包含主要代码的 Dart 文件和包含意图捕获代码的 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 的唯一方法是将其从之前的应用列表中删除。然后,直到那时我才得到正确的输出。

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

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

在您的 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.