我有一个 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
是相关的。如果它设置为 singleTop
,Activity
将收到对 onNewIntent(Intent)
的调用:
Note that
getIntent()
still returns the original Intent. You can usesetIntent(Intent)
to update it to this new Intent.
关于android - 如何让我的 flutter 应用程序意识到它是以不同的 Intent 启动的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49291697/