Unverified Commit 73275f08 authored by Gonçalo Palma's avatar Gonçalo Palma Committed by GitHub

Change reference to current class when setting the MethodChannel (#48806)

* Change reference to current class when setting the MethodChannel

* Added reference to MethodChannel and detach it from the engine
parent 25100369
......@@ -16,10 +16,16 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
/** {{pluginClass}} */
public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}");
channel.setMethodCallHandler(new {{pluginClass}}());
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}");
channel.setMethodCallHandler(this);
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
......@@ -47,6 +53,7 @@ public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler {
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
{{/useAndroidEmbeddingV2}}
......
......@@ -16,9 +16,15 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
/** {{pluginClass}} */
public class {{pluginClass}}: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}")
channel.setMethodCallHandler({{pluginClass}}());
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}")
channel.setMethodCallHandler(this);
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
......@@ -47,6 +53,7 @@ public class {{pluginClass}}: FlutterPlugin, MethodCallHandler {
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
{{/useAndroidEmbeddingV2}}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment