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
This commit is contained in:
Gonçalo Palma
2020-01-26 13:34:04 +01:00
committed by GitHub
parent 251003690f
commit 73275f0815
2 changed files with 18 additions and 4 deletions

View File

@@ -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}}

View File

@@ -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}}