Interface FlutterPlugin
-
public interface FlutterPlugin
Interface 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
MethodChannel
to communicate between platform code and Dart code.A Flutter plugin has a lifecycle. First, a developer must add a
FlutterPlugin
to an instance ofFlutterEngine
. To do this, obtain aPluginRegistry
withFlutterEngine.getPlugins()
, then callPluginRegistry.add(FlutterPlugin)
, passing the instance of the Flutter plugin. During the call toPluginRegistry.add(FlutterPlugin)
, theFlutterEngine
will invokeonAttachedToEngine(FlutterPluginBinding)
on the givenFlutterPlugin
. If theFlutterPlugin
is removed from theFlutterEngine
viaPluginRegistry.remove(Class)
, or if theFlutterEngine
is destroyed, theFlutterEngine
will invokeonDetachedFromEngine(FlutterPluginBinding)
on the givenFlutterPlugin
.Once a
FlutterPlugin
is attached to aFlutterEngine
, the plugin's code is permitted to access and invoke methods on resources within theFlutterPlugin.FlutterPluginBinding
that theFlutterEngine
gave to theFlutterPlugin
inonAttachedToEngine(FlutterPluginBinding)
. This includes, for example, the applicationContext
for the running app.The
FlutterPlugin.FlutterPluginBinding
provided inonAttachedToEngine(FlutterPluginBinding)
is no longer valid after the execution ofonDetachedFromEngine(FlutterPluginBinding)
. Do not access any properties of theFlutterPlugin.FlutterPluginBinding
after the completion ofonDetachedFromEngine(FlutterPluginBinding)
.To register a
MethodChannel
, obtain aBinaryMessenger
via 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
Activity
that is displaying a Flutter experience, or may need to react toActivity
lifecycle events, e.g.,onCreate()
,onStart()
,onResume()
,onPause()
,onStop()
,onDestroy()
. Any such plugin should implementActivityAware
in addition to implementingFlutterPlugin
.ActivityAware
provides callback hooks that expose access to an associatedActivity
and 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 aActivity
s may come and go over time, thus requiring plugins to cleanup resources and recreate those resources as theActivity
comes and goes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
FlutterPlugin.FlutterAssets
Provides Flutter plugins with access to Flutter asset information.static class
FlutterPlugin.FlutterPluginBinding
Resources made available to all plugins registered with a givenFlutterEngine
.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
onAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding)
ThisFlutterPlugin
has been associated with aFlutterEngine
instance.void
onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding)
ThisFlutterPlugin
has been removed from aFlutterEngine
instance.
-
-
-
Method Detail
-
onAttachedToEngine
void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding)
ThisFlutterPlugin
has been associated with aFlutterEngine
instance.Relevant resources that this
FlutterPlugin
may need are provided via thebinding
. Thebinding
may be cached and referenced untilonDetachedFromEngine(FlutterPluginBinding)
is invoked and returns.
-
onDetachedFromEngine
void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding)
ThisFlutterPlugin
has been removed from aFlutterEngine
instance.The
binding
passed to this method is the same instance that was passed inonAttachedToEngine(FlutterPluginBinding)
. It is provided again in this method as a convenience. Thebinding
may be referenced during the execution of this method, but it must not be cached or referenced after this method returns.FlutterPlugin
s should release all resources in this method.
-
-