Interface FlutterPlugin
-
public interface FlutterPluginInterface to be implemented by all Flutter plugins.A Flutter plugin allows Flutter developers to interact with a host platform, e.g., Android and iOS, via Dart code. It includes platform code, as well as Dart code. A plugin author is responsible for setting up an appropriate
MethodChannelto communicate between platform code and Dart code.A Flutter plugin has a lifecycle. First, a developer must add a
FlutterPluginto an instance ofFlutterEngine. To do this, obtain aPluginRegistrywithFlutterEngine.getPlugins(), then callPluginRegistry.add(FlutterPlugin), passing the instance of the Flutter plugin. During the call toPluginRegistry.add(FlutterPlugin), theFlutterEnginewill invokeonAttachedToEngine(FlutterPluginBinding)on the givenFlutterPlugin. If theFlutterPluginis removed from theFlutterEngineviaPluginRegistry.remove(Class), or if theFlutterEngineis destroyed, theFlutterEnginewill invokeonDetachedFromEngine(FlutterPluginBinding)on the givenFlutterPlugin.Once a
FlutterPluginis attached to aFlutterEngine, the plugin's code is permitted to access and invoke methods on resources within theFlutterPlugin.FlutterPluginBindingthat theFlutterEnginegave to theFlutterPlugininonAttachedToEngine(FlutterPluginBinding). This includes, for example, the applicationContextfor the running app.The
FlutterPlugin.FlutterPluginBindingprovided inonAttachedToEngine(FlutterPluginBinding)is no longer valid after the execution ofonDetachedFromEngine(FlutterPluginBinding). Do not access any properties of theFlutterPlugin.FlutterPluginBindingafter the completion ofonDetachedFromEngine(FlutterPluginBinding).To register a
MethodChannel, obtain aBinaryMessengervia theFlutterPlugin.FlutterPluginBinding.An Android Flutter plugin may require access to app resources or other artifacts that can only be retrieved through a
Context. Developers can access the application context viaFlutterPlugin.FlutterPluginBinding.getApplicationContext().Some plugins may require access to the
Activitythat is displaying a Flutter experience, or may need to react toActivitylifecycle events, e.g.,onCreate(),onStart(),onResume(),onPause(),onStop(),onDestroy(). Any such plugin should implementActivityAwarein addition to implementingFlutterPlugin.ActivityAwareprovides callback hooks that expose access to an associatedActivityand itsLifecycle. All plugins must respect the possibility that a Flutter experience may never be associated with anActivity, e.g., when Flutter is used for background behavior. Additionally, all plugins must respect that aActivitys may come and go over time, thus requiring plugins to cleanup resources and recreate those resources as theActivitycomes and goes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceFlutterPlugin.FlutterAssetsProvides Flutter plugins with access to Flutter asset information.static classFlutterPlugin.FlutterPluginBindingResources made available to all plugins registered with a givenFlutterEngine.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidonAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding)ThisFlutterPluginhas been associated with aFlutterEngineinstance.voidonDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding)ThisFlutterPluginhas been removed from aFlutterEngineinstance.
-
-
-
Method Detail
-
onAttachedToEngine
void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding)ThisFlutterPluginhas been associated with aFlutterEngineinstance.Relevant resources that this
FlutterPluginmay need are provided via thebinding. Thebindingmay be cached and referenced untilonDetachedFromEngine(FlutterPluginBinding)is invoked and returns.
-
onDetachedFromEngine
void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding)ThisFlutterPluginhas been removed from aFlutterEngineinstance.The
bindingpassed to this method is the same instance that was passed inonAttachedToEngine(FlutterPluginBinding). It is provided again in this method as a convenience. Thebindingmay be referenced during the execution of this method, but it must not be cached or referenced after this method returns.FlutterPlugins should release all resources in this method.
-
-