Get activity reference in flutter plugin
Solution 1
Found the solution here.
Implement ActivityAware
and one of its methods is
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.activity = binding.activity;
}
Solution 2
Note:
you can get reference to activity by implementing ActivityAware interface but if you setMethodCallHandler(...)
in onAttachToEngine()
method onAttachToActivity()
will never be called and you can never get access to activity
take a look at below examples
WHAT DOES NOT WORK :
in below examples onAttachToActivity()
is never called
class AndroidLongTaskPlugin : FlutterPlugin, ActivityAware {
private var activity: FlutterActivity? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
//activity is null here
//also onAttachToActivity will never be called because we are calling setMethodHandler here
channel = MethodChannel(binaryMessenger, CHANNEL_NAME)
channel.setMethodCallHandler { call, result ->
//our code
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity as FlutterActivity
}
//rest of the methods
}
HERE IS A WORKING EXAMPLE :
class MyPlugin : FlutterPlugin, ActivityAware {
private var activity: FlutterActivity? = null
private var binaryMessenger: BinaryMessenger? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
binaryMessenger = flutterPluginBinding.binaryMessenger
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
Log.d("DART/NATIVE", "onDetachedFromEngine")
channel?.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
Log.d("DART/NATIVE", "onAttachedToActivity")
activity = binding.activity as FlutterActivity
//here we have access to activity
//also make sure to setMethodCallHandler here
channel.setMethodCallHandler { call, result ->
//our code
}
}
//rest of the methods
}
Neeraj
Updated on December 17, 2022Comments
-
Neeraj over 1 year
When I created a flutter plugin, there are two methods in the plugin class by default:
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)
and
fun registerWith(registrar: Registrar)
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.
Now, I need to start another activity from here, with
activity.startActivityForResult()
. It is possible to get a reference to the activity inregisterWith(registrar: Registrar)
usingregistrar.activity()
. How can I do this in the methodonAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)
? -
Satya Attili over 3 years@Neeraj, I tried above solution, but surprisingly on method call the activity is null. I assigned activity in AttachedToActivity callback and after immediate onMethodCall, the this.activity is null. Any suggestions?
-
cwallenwein over 3 yearsRead more in the documentation
-
Schnodderbalken over 3 years@Satya Attili I have exactly the same problem. Have you found a solution?
-
alireza easazade about 3 years@Alexufo. check this library out pub.dev/packages/android_long_task
-
Alexufo about 3 yearsDo you know where I can attach startActivityForResult? I want open new activity.
-
Alexufo about 3 yearsI found solution bitbucket.org/prathap_kumar/mvbarcodescan/raw/…
-
Alexufo about 3 yearsIn my case in
onAttachedToEngine
this code workMethodChannel channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), channelName); channel.setMethodCallHandler(this);
And ononAttachedToActivity
I useactivity = activityPluginBinding.getActivity(); activityPluginBinding.addActivityResultListener(this);
-
Ben Butterworth over 2 yearsUnder what conditions are you trying @SatyaAttili? If you are using a background message handler in a Service or static broadcast receiver, then the Activity is not yet created, so
onAttachedToActivity
was never called. Sothis.activity
would be null.