diff --git a/packages/flutter_tools/templates/plugin/android-java.tmpl/src/main/java/androidIdentifier/pluginClass.java.tmpl b/packages/flutter_tools/templates/plugin/android-java.tmpl/src/main/java/androidIdentifier/pluginClass.java.tmpl index c2bdb80e6c..6616555623 100644 --- a/packages/flutter_tools/templates/plugin/android-java.tmpl/src/main/java/androidIdentifier/pluginClass.java.tmpl +++ b/packages/flutter_tools/templates/plugin/android-java.tmpl/src/main/java/androidIdentifier/pluginClass.java.tmpl @@ -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}} diff --git a/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/src/main/kotlin/androidIdentifier/pluginClass.kt.tmpl b/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/src/main/kotlin/androidIdentifier/pluginClass.kt.tmpl index 2e7596aad4..6d1b58c798 100644 --- a/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/src/main/kotlin/androidIdentifier/pluginClass.kt.tmpl +++ b/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/src/main/kotlin/androidIdentifier/pluginClass.kt.tmpl @@ -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}}