在flutter插件中获取活动参考 [英] Get activity reference in flutter plugin
问题描述
当我创建一个flutter插件时,默认情况下,插件类中有两种方法:
When I created a flutter plugin, there are two methods in the plugin class by default:
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)
和
fun registerWith(registrar: Registrar)
对该文件的评论说:
It is encouraged to share logic between onAttachedToEngine and registerWith to keep them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called depending on the user's project. onAttachedToEngine or registerWith must both be defined in the same class.
The comment on the file says :
It is encouraged to share logic between onAttachedToEngine and registerWith to keep them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called depending on the user's project. onAttachedToEngine or registerWith must both be defined in the same class.
现在,我需要使用activity.startActivityForResult()
从此处开始另一个活动.
可以使用registrar.activity()
引用registerWith(registrar: Registrar)
中的活动.如何在onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)
方法中做到这一点?
Now, I need to start another activity from here, with activity.startActivityForResult()
.
It is possible to get a reference to the activity in registerWith(registrar: Registrar)
using registrar.activity()
. How can I do this in the method onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)
?
推荐答案
Found the solution here.
Implement ActivityAware
and one of its methods is
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.activity = binding.activity;
}
这篇关于在flutter插件中获取活动参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!