public class FlutterFragment extends android.support.v4.app.Fragment
Fragment
which displays a Flutter UI that takes up all available Fragment
space.
WARNING: THIS CLASS IS EXPERIMENTAL. DO NOT SHIP A DEPENDENCY ON THIS CODE. IF YOU USE IT, WE WILL BREAK YOU.
Using a FlutterFragment
requires forwarding a number of calls from an Activity
to
ensure that the internal Flutter app behaves as expected:
Activity.onPostResume()
Activity.onBackPressed()
Activity.onRequestPermissionsResult(int, String[], int[])
()}Activity.onNewIntent(Intent)
()}Activity.onUserLeaveHint()
Activity.onTrimMemory(int)
Activity
for a result from this Fragment
, be sure
to invoke Fragment.startActivityForResult(Intent, int)
rather than
Activity.startActivityForResult(Intent, int)
. If the Activity
version
of the method is invoked then this Fragment
will never receive its
Fragment.onActivityResult(int, int, Intent)
callback.
If convenient, consider using a FlutterActivity
instead of a FlutterFragment
to
avoid the work of forwarding calls.
If Flutter is needed in a location that can only use a View
, consider using a
FlutterView
. Using a FlutterView
requires forwarding some calls from an
Activity
, as well as forwarding lifecycle calls from an Activity
or a
Fragment
.
Modifier and Type | Class and Description |
---|---|
static class |
FlutterFragment.Builder
Builder that creates a new
FlutterFragment with arguments that correspond
to the values set on this Builder . |
static interface |
FlutterFragment.FlutterEngineProvider
Provides a
FlutterEngine instance to be used by a FlutterFragment . |
android.support.v4.app.Fragment.InstantiationException, android.support.v4.app.Fragment.SavedState
Modifier and Type | Field and Description |
---|---|
protected static String |
ARG_APP_BUNDLE_PATH |
protected static String |
ARG_DART_ENTRYPOINT |
protected static String |
ARG_FLUTTER_INITIALIZATION_ARGS |
protected static String |
ARG_FLUTTERVIEW_RENDER_MODE |
protected static String |
ARG_FLUTTERVIEW_TRANSPARENCY_MODE |
protected static String |
ARG_INITIAL_ROUTE |
Constructor and Description |
---|
FlutterFragment() |
Modifier and Type | Method and Description |
---|---|
protected FlutterEngine |
createFlutterEngine(Context context)
Hook for subclasses to return a
FlutterEngine with whatever configuration
is desired. |
protected String |
getAppBundlePath()
Returns the file path to the desired Flutter app's bundle of code.
|
protected String |
getDartEntrypointFunctionName()
Returns the name of the Dart method that this
FlutterFragment should execute to
start a Flutter app. |
FlutterEngine |
getFlutterEngine()
The
FlutterEngine that backs the Flutter content presented by this Fragment . |
protected String |
getInitialRoute()
Returns the initial route that should be rendered within Flutter, once the Flutter app starts.
|
protected FlutterView.RenderMode |
getRenderMode()
|
protected FlutterView.TransparencyMode |
getTransparencyMode()
Returns the desired
FlutterView.TransparencyMode for the FlutterView displayed in
this FlutterFragment . |
void |
onActivityResult(int requestCode,
int resultCode,
Intent data)
A result has been returned after an invocation of
Fragment.startActivityForResult(Intent, int) . |
void |
onAttach(Context context) |
void |
onBackPressed()
The hardware back button was pressed.
|
View |
onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) |
void |
onDestroyView() |
void |
onDetach() |
protected void |
onFirstFrameRendered()
Invoked after the
FlutterView within this FlutterFragment renders its first
frame. |
void |
onLowMemory()
Callback invoked when memory is low.
|
void |
onNewIntent(Intent intent)
|
void |
onPause() |
void |
onPostResume() |
void |
onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults)
The result of a permission request has been received.
|
void |
onResume() |
void |
onStop() |
void |
onTrimMemory(int level)
Callback invoked when memory is low.
|
void |
onUserLeaveHint()
|
protected boolean |
retainFlutterEngineAfterFragmentDestruction()
Returns true if the
FlutterEngine within this FlutterFragment should outlive
the FlutterFragment , itself. |
dump, equals, getActivity, getAllowEnterTransitionOverlap, getAllowReturnTransitionOverlap, getArguments, getChildFragmentManager, getContext, getEnterTransition, getExitTransition, getFragmentManager, getHost, getId, getLayoutInflater, getLayoutInflater, getLifecycle, getLoaderManager, getParentFragment, getReenterTransition, getResources, getRetainInstance, getReturnTransition, getSharedElementEnterTransition, getSharedElementReturnTransition, getString, getString, getTag, getTargetFragment, getTargetRequestCode, getText, getUserVisibleHint, getView, getViewLifecycleOwner, getViewLifecycleOwnerLiveData, getViewModelStore, hashCode, hasOptionsMenu, instantiate, instantiate, isAdded, isDetached, isHidden, isInLayout, isMenuVisible, isRemoving, isResumed, isStateSaved, isVisible, onActivityCreated, onAttach, onAttachFragment, onConfigurationChanged, onContextItemSelected, onCreate, onCreateAnimation, onCreateAnimator, onCreateContextMenu, onCreateOptionsMenu, onDestroy, onDestroyOptionsMenu, onGetLayoutInflater, onHiddenChanged, onInflate, onInflate, onMultiWindowModeChanged, onOptionsItemSelected, onOptionsMenuClosed, onPictureInPictureModeChanged, onPrepareOptionsMenu, onSaveInstanceState, onStart, onViewCreated, onViewStateRestored, postponeEnterTransition, registerForContextMenu, requestPermissions, requireActivity, requireContext, requireFragmentManager, requireHost, setAllowEnterTransitionOverlap, setAllowReturnTransitionOverlap, setArguments, setEnterSharedElementCallback, setEnterTransition, setExitSharedElementCallback, setExitTransition, setHasOptionsMenu, setInitialSavedState, setMenuVisibility, setReenterTransition, setRetainInstance, setReturnTransition, setSharedElementEnterTransition, setSharedElementReturnTransition, setTargetFragment, setUserVisibleHint, shouldShowRequestPermissionRationale, startActivity, startActivity, startActivityForResult, startActivityForResult, startIntentSenderForResult, startPostponedEnterTransition, toString, unregisterForContextMenu
protected static final String ARG_DART_ENTRYPOINT
protected static final String ARG_INITIAL_ROUTE
protected static final String ARG_APP_BUNDLE_PATH
protected static final String ARG_FLUTTER_INITIALIZATION_ARGS
protected static final String ARG_FLUTTERVIEW_RENDER_MODE
protected static final String ARG_FLUTTERVIEW_TRANSPARENCY_MODE
@Nullable public FlutterEngine getFlutterEngine()
FlutterEngine
that backs the Flutter content presented by this Fragment
.FlutterEngine
held by this Fragment
public void onAttach(Context context)
onAttach
in class android.support.v4.app.Fragment
@Nullable protected FlutterEngine createFlutterEngine(@NonNull Context context)
FlutterEngine
with whatever configuration
is desired.
This method takes precedence for creation of a FlutterEngine
over any owning
Activity
that may implement FlutterFragment.FlutterEngineProvider
.
Consider returning a cached FlutterEngine
instance from this method to avoid the
typical warm-up time that a new FlutterEngine
instance requires.
If null is returned then a new default FlutterEngine
will be created to back this
FlutterFragment
.
@Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
onCreateView
in class android.support.v4.app.Fragment
@Nullable protected String getInitialRoute()
null
, which signifies a route of "/" in Flutter.@NonNull protected String getAppBundlePath()
FlutterMain.findAppBundlePath(Context)
.@NonNull protected String getDartEntrypointFunctionName()
FlutterFragment
should execute to
start a Flutter app.
Defaults to "main".@NonNull protected FlutterView.RenderMode getRenderMode()
FlutterView.RenderMode
for the FlutterView
displayed in
this FlutterFragment
.
Defaults to FlutterView.RenderMode.surface
.@NonNull protected FlutterView.TransparencyMode getTransparencyMode()
FlutterView.TransparencyMode
for the FlutterView
displayed in
this FlutterFragment
.
Defaults to FlutterView.TransparencyMode.transparent
.
public void onResume()
onResume
in class android.support.v4.app.Fragment
public void onPostResume()
public void onPause()
onPause
in class android.support.v4.app.Fragment
public void onStop()
onStop
in class android.support.v4.app.Fragment
public void onDestroyView()
onDestroyView
in class android.support.v4.app.Fragment
public void onDetach()
onDetach
in class android.support.v4.app.Fragment
protected boolean retainFlutterEngineAfterFragmentDestruction()
FlutterEngine
within this FlutterFragment
should outlive
the FlutterFragment
, itself.
Defaults to false. This method can be overridden in subclasses to retain the
FlutterEngine
.public void onBackPressed()
Activity.onBackPressed()
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
Activity.onRequestPermissionsResult(int, String[], int[])
onRequestPermissionsResult
in class android.support.v4.app.Fragment
requestCode
- identifier passed with the initial permission requestpermissions
- permissions that were requestedgrantResults
- permission grants or denialspublic void onNewIntent(@NonNull Intent intent)
Activity
that currently owns this
Fragment
.
See Activity.onNewIntent(Intent)
intent
- new Intentpublic void onActivityResult(int requestCode, int resultCode, Intent data)
Fragment.startActivityForResult(Intent, int)
.onActivityResult
in class android.support.v4.app.Fragment
requestCode
- request code sent with Fragment.startActivityForResult(Intent, int)
resultCode
- code representing the result of the Activity
that was launcheddata
- any corresponding return data, held within an Intent
public void onUserLeaveHint()
Activity
that owns this Fragment
is about to go to the background
as the result of a user's choice/action, i.e., not as the result of an OS decision.
See Activity.onUserLeaveHint()
public void onTrimMemory(int level)
level
- levelpublic void onLowMemory()
onLowMemory
in interface ComponentCallbacks
onLowMemory
in class android.support.v4.app.Fragment
protected void onFirstFrameRendered()
FlutterView
within this FlutterFragment
renders its first
frame.
The owning Activity
is also sent this message, if it implements
OnFirstFrameRenderedListener
. This method is invoked before the Activity
's
version.