text
stringlengths
1
372
// using a cached FlutterEngine.
val flutterFragment = FlutterFragment.withCachedEngine("my_engine_id")
.shouldattachenginetoactivity(false)
.build()
<code_end>
passing false to the shouldAttachEngineToActivity()
builder method prevents flutter from interacting with
the surrounding activity. the default value is true,
which allows flutter and flutter plugins to interact with the
surrounding activity.
info note
some plugins might expect or require an activity reference.
ensure that none of your plugins require an activity
before you disable access.
<topic_end>
<topic_start>
add a flutter view to an android app
warning warning
integrating via a FlutterView
is advanced usage and requires manually creating custom, application specific
bindings.
integrating via a FlutterView
requires a bit more work than via FlutterActivity and FlutterFragment previously
described.
fundamentally, the flutter framework on the dart side requires access to various
activity-level events and lifecycles to function. since the FlutterView (which
is an android.view.View)
can be added to any activity which is owned by the developer’s application
and since the FlutterView doesn’t have access to activity level events, the
developer must bridge those connections manually to the FlutterEngine.
how you choose to feed your application’s activities’ events to the FlutterView
will be specific to your application.
<topic_end>
<topic_start>
a sample
unlike the guides for FlutterActivity and FlutterFragment, the FlutterView
integration could be better demonstrated with a sample project.
a sample project is at https://github.com/flutter/samples/tree/main/add_to_app/android_view
to document a simple FlutterView integration where FlutterViews are used
for some of the cells in a RecycleView list of cards as seen in the gif above.
<topic_end>
<topic_start>
general approach
the general gist of the FlutterView-level integration is that you must recreate
the various interactions between your activity, the FlutterView
and the FlutterEngine
present in the FlutterActivityAndFragmentDelegate
in your own application’s code. the connections made in the FlutterActivityAndFragmentDelegate
are done automatically when using a FlutterActivity
or a FlutterFragment,
but since the FlutterView
in this case is being added to an activity or fragment in your application,
you must recreate the connections manually. otherwise, the FlutterView
will not render anything or have other missing functionalities.
a sample FlutterViewEngine
class shows one such possible implementation of an application-specific
connection between an activity, a FlutterView
and a FlutterEngine.
<topic_end>
<topic_start>
APIs to implement
the absolute minimum implementation needed for flutter to draw anything at all
is to:
the reverse detachFromFlutterEngine and other lifecycle methods on the LifecycleChannel
class must also be called to not leak resources when the FlutterView or activity
is no longer visible.
in addition, see the remaining implementation in the FlutterViewEngine
demo class or in the FlutterActivityAndFragmentDelegate
to ensure a correct functioning of other features such as clipboards, system
UI overlay, plugins etc.
<topic_end>
<topic_start>
manage plugins and dependencies in add-to-app
this guide describes how to set up your project to consume
plugins and how to manage your gradle library dependencies
between your existing android app and your flutter module’s plugins.
<topic_end>
<topic_start>
a. simple scenario
in the simple cases:
there are no additional steps needed. your add-to-app
module will work the same way as a full-Flutter app.
whether you integrate using android studio,
gradle subproject or AARs,
transitive android gradle libraries are automatically
bundled as needed into your outer existing app.
<topic_end>
<topic_start>
b. plugins needing project edits
some plugins require you to make some edits to the
android side of your project.
for example, the integration instructions for the
firebase_crashlytics plugin require manual
edits to your android wrapper project’s build.gradle file.
for full-Flutter apps, these edits are done in your
flutter project’s /android/ directory.
in the case of a flutter module, there are only dart
files in your module project. perform those android
gradle file edits on your outer, existing android
app rather than in your flutter module.