Get activity reference in flutter plugin

5,686

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
}


Share:
5,686
Neeraj
Author by

Neeraj

Updated on December 17, 2022

Comments

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

  • Satya Attili
    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
    cwallenwein over 3 years
    Read more in the documentation
  • Schnodderbalken
    Schnodderbalken over 3 years
    @Satya Attili I have exactly the same problem. Have you found a solution?
  • alireza easazade
    alireza easazade about 3 years
    @Alexufo. check this library out pub.dev/packages/android_long_task
  • Alexufo
    Alexufo about 3 years
    Do you know where I can attach startActivityForResult? I want open new activity.
  • Alexufo
    Alexufo about 3 years
  • Alexufo
    Alexufo about 3 years
    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);
  • Ben Butterworth
    Ben Butterworth over 2 years
    Under 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. So this.activity would be null.