Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get activity reference in flutter plugin

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) ?

like image 336
Neeraj Avatar asked Jan 23 '20 22:01

Neeraj


People also ask

How do you get activity on Flutter plugin?

Show activity on this post. private Context mContext; // Instance variable for context // .... @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { onAttach(flutterPluginBinding. getApplicationContext(),flutterPluginBinding.

What is FlutterActivity in Flutter?

FlutterActivity is the new API that now replaces this class. See https://flutter.dev/go/android-project-migration for more migration details. Deprecated base class for activities that use Flutter.

What is v2 embedding?

The embedded v2 provides better support for things like background execution (firebase messaging for instance. Checkout the changeLog). If you are developing a plugin, you should consider starting with embedded v2. Existing packages are already migrated or migrating. Follow this answer to receive notifications.


1 Answers

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
}


like image 83
alireza easazade Avatar answered Sep 20 '22 16:09

alireza easazade