8

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 in registerWith(registrar: Registrar) using registrar.activity(). How can I do this in the method onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) ?

Neeraj
  • 1,895
  • 16
  • 31

2 Answers2

13

Found the solution here.
Implement ActivityAware and one of its methods is

 override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    this.activity = binding.activity;
  }
Neeraj
  • 1,895
  • 16
  • 31
  • 1
    @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? – Satya Attili Oct 06 '20 at 17:27
  • Read more in [the documentation](https://api.flutter.dev/javadoc/io/flutter/embedding/engine/plugins/FlutterPlugin.html) – ChrisWallenwein Oct 31 '20 at 14:51
  • @Satya Attili I have exactly the same problem. Have you found a solution? – Schnodderbalken Dec 02 '20 at 12:44
1

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
}


alireza easazade
  • 1,577
  • 4
  • 16
  • 27
  • have you a project as working example? – Alexufo Apr 17 '21 at 18:18
  • 1
    @Alexufo. check this library out https://pub.dev/packages/android_long_task – alireza easazade Apr 17 '21 at 20:02
  • Do you know where I can attach startActivityForResult? I want open new activity. – Alexufo Apr 22 '21 at 04:43
  • I found solution https://bitbucket.org/prathap_kumar/mvbarcodescan/raw/7a231a8235e3eda5639b54954f3c5822199b5249/android/src/main/java/com/mv/mvbarcodescan/MvbarcodescanPlugin.java – Alexufo Apr 22 '21 at 23:31
  • In my case in `onAttachedToEngine` this code work `MethodChannel channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), channelName); channel.setMethodCallHandler(this);` And on `onAttachedToActivity` I use `activity = activityPluginBinding.getActivity(); activityPluginBinding.addActivityResultListener(this);` – Alexufo Apr 23 '21 at 02:06