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; ...@@ -16,10 +16,16 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
/** {{pluginClass}} */ /** {{pluginClass}} */
public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler { 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 @Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}"); channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}");
channel.setMethodCallHandler(new {{pluginClass}}()); channel.setMethodCallHandler(this);
} }
// This static function is optional and equivalent to onAttachedToEngine. It supports the old // This static function is optional and equivalent to onAttachedToEngine. It supports the old
...@@ -47,6 +53,7 @@ public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler { ...@@ -47,6 +53,7 @@ public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler {
@Override @Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
} }
} }
{{/useAndroidEmbeddingV2}} {{/useAndroidEmbeddingV2}}
......
...@@ -16,9 +16,15 @@ import io.flutter.plugin.common.PluginRegistry.Registrar ...@@ -16,9 +16,15 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
/** {{pluginClass}} */ /** {{pluginClass}} */
public class {{pluginClass}}: FlutterPlugin, MethodCallHandler { 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) { override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}") channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "{{projectName}}")
channel.setMethodCallHandler({{pluginClass}}()); channel.setMethodCallHandler(this);
} }
// This static function is optional and equivalent to onAttachedToEngine. It supports the old // This static function is optional and equivalent to onAttachedToEngine. It supports the old
...@@ -47,6 +53,7 @@ public class {{pluginClass}}: FlutterPlugin, MethodCallHandler { ...@@ -47,6 +53,7 @@ public class {{pluginClass}}: FlutterPlugin, MethodCallHandler {
} }
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
} }
} }
{{/useAndroidEmbeddingV2}} {{/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