Class FlutterFragmentActivity

All Implemented Interfaces:
ComponentCallbacks, ComponentCallbacks2, KeyEvent.Callback, LayoutInflater.Factory, LayoutInflater.Factory2, View.OnCreateContextMenuListener, Window.Callback, androidx.activity.OnBackPressedDispatcherOwner,,, androidx.core.view.KeyEventDispatcher.Component, androidx.lifecycle.LifecycleOwner, androidx.lifecycle.ViewModelStoreOwner, androidx.savedstate.SavedStateRegistryOwner, FlutterEngineConfigurator, FlutterEngineProvider

public class FlutterFragmentActivity extends implements FlutterEngineProvider, FlutterEngineConfigurator
A Flutter Activity that is based upon FragmentActivity.

FlutterFragmentActivity exists because there are some Android APIs in the ecosystem that only accept a FragmentActivity. If a FragmentActivity is not required, you should consider using a regular FlutterActivity instead, because FlutterActivity is considered to be the standard, canonical implementation of a Flutter Activity.

  • Field Details


      public static final int FRAGMENT_CONTAINER_ID
  • Constructor Details

    • FlutterFragmentActivity

      public FlutterFragmentActivity()
  • Method Details

    • createDefaultIntent

      @NonNull public static Intent createDefaultIntent(@NonNull Context launchContext)
      Creates an Intent that launches a FlutterFragmentActivity, which executes a main() Dart entrypoint, and displays the "/" route as Flutter's initial route.
    • withNewEngine

      @NonNull public static FlutterFragmentActivity.NewEngineIntentBuilder withNewEngine()
      Creates an FlutterFragmentActivity.NewEngineIntentBuilder, which can be used to configure an Intent to launch a FlutterFragmentActivity that internally creates a new FlutterEngine using the desired Dart entrypoint, initial route, etc.
    • withCachedEngine

      @NonNull public static FlutterFragmentActivity.CachedEngineIntentBuilder withCachedEngine(@NonNull String cachedEngineId)
      Creates a FlutterFragmentActivity.CachedEngineIntentBuilder, which can be used to configure an Intent to launch a FlutterFragmentActivity that internally uses an existing FlutterEngine that is cached in FlutterEngineCache.
    • withNewEngineInGroup

      public static FlutterFragmentActivity.NewEngineInGroupIntentBuilder withNewEngineInGroup(@NonNull String engineGroupId)
      Creates a FlutterFragmentActivity.NewEngineInGroupIntentBuilder, which can be used to configure an Intent to launch a FlutterFragmentActivity that internally uses an existing FlutterEngineGroup that is cached in FlutterEngineGroupCache.
      engineGroupId - A cached engine group ID.
      The builder.
    • onCreate

      protected void onCreate(@Nullable Bundle savedInstanceState)
      onCreate in class
    • createFlutterFragment

      @NonNull protected FlutterFragment createFlutterFragment()
      Creates the instance of the FlutterFragment that this FlutterFragmentActivity displays.

      Subclasses may override this method to return a specialization of FlutterFragment.

    • onPostResume

      public void onPostResume()
      onPostResume in class
    • onNewIntent

      protected void onNewIntent(@NonNull Intent intent)
      onNewIntent in class
    • onBackPressed

      public void onBackPressed()
      onBackPressed in class androidx.activity.ComponentActivity
    • onRequestPermissionsResult

      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
      Specified by:
      onRequestPermissionsResult in interface
      onRequestPermissionsResult in class
    • onUserLeaveHint

      public void onUserLeaveHint()
      onUserLeaveHint in class Activity
    • onTrimMemory

      public void onTrimMemory(int level)
      Specified by:
      onTrimMemory in interface ComponentCallbacks2
      onTrimMemory in class Activity
    • onActivityResult

      protected void onActivityResult(int requestCode, int resultCode, Intent data)
      onActivityResult in class
    • getFlutterEngine

      @Nullable protected FlutterEngine getFlutterEngine()
    • shouldDestroyEngineWithHost

      public boolean shouldDestroyEngineWithHost()
      Returns false if the FlutterEngine backing this FlutterFragmentActivity should outlive this FlutterFragmentActivity, or true to be destroyed when the FlutterFragmentActivity is destroyed.

      The default value is true in cases where FlutterFragmentActivity created its own FlutterEngine, and false in cases where a cached FlutterEngine was provided.

    • shouldAttachEngineToActivity

      protected boolean shouldAttachEngineToActivity()
      Hook for subclasses to control whether or not the FlutterFragment within this Activity automatically attaches its FlutterEngine to this Activity.

      For an explanation of why this control exists, see FlutterFragment.shouldAttachEngineToActivity().

      This property is controlled with a protected method instead of an Intent argument because the only situation where changing this value would help, is a situation in which FlutterFragmentActivity is being subclassed to utilize a custom and/or cached FlutterEngine.

      Defaults to true.

    • shouldHandleDeeplinking

      protected boolean shouldHandleDeeplinking()
      Whether to handle the deeplinking from the Intent automatically if the getInitialRoute returns null.

      The default implementation looks <meta-data> called FlutterActivityLaunchConfigs.HANDLE_DEEPLINKING_META_DATA_KEY within the Android manifest definition for this FlutterFragmentActivity.

    • provideFlutterEngine

      @Nullable public FlutterEngine provideFlutterEngine(@NonNull Context context)
      Hook for subclasses to easily provide a custom FlutterEngine.
      Specified by:
      provideFlutterEngine in interface FlutterEngineProvider
      context - The current context. e.g. An activity.
      The Flutter engine.
    • configureFlutterEngine

      public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine)
      Hook for subclasses to easily configure a FlutterEngine.

      This method is called after provideFlutterEngine(Context).

      All plugins listed in the app's pubspec are registered in the base implementation of this method unless the FlutterEngine for this activity was externally created. To avoid the automatic plugin registration for implicitly created FlutterEngines, override this method without invoking super(). To keep automatic plugin registration and further configure the FlutterEngine, override this method, invoke super(), and then configure the FlutterEngine as desired.

      Specified by:
      configureFlutterEngine in interface FlutterEngineConfigurator
      flutterEngine - The Flutter engine.
    • cleanUpFlutterEngine

      public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine)
      Hook for the host to cleanup references that were established in configureFlutterEngine(FlutterEngine) before the host is destroyed or detached.

      This method is called in FragmentActivity.onDestroy().

      Specified by:
      cleanUpFlutterEngine in interface FlutterEngineConfigurator
      flutterEngine - The Flutter engine.
    • getAppBundlePath

      @NonNull protected String getAppBundlePath()
      A custom path to the bundle that contains this Flutter app's resources, e.g., Dart code snapshots.

      When this FlutterFragmentActivity is run by Flutter tooling and a data String is included in the launching Intent, that data String is interpreted as an app bundle path.

      When otherwise unspecified, the value is null, which defaults to the app bundle path defined in FlutterLoader.findAppBundlePath().

      Subclasses may override this method to return a custom app bundle path.

    • getMetaData

      @Nullable protected Bundle getMetaData() throws PackageManager.NameNotFoundException
      Retrieves the meta data specified in the AndroidManifest.xml.
    • getDartEntrypointFunctionName

      @NonNull public String getDartEntrypointFunctionName()
      The Dart entrypoint that will be executed as soon as the Dart snapshot is loaded.

      This preference can be controlled by setting a <meta-data> called FlutterActivityLaunchConfigs.DART_ENTRYPOINT_META_DATA_KEY within the Android manifest definition for this FlutterFragmentActivity.

      Subclasses may override this method to directly control the Dart entrypoint.

    • getDartEntrypointArgs

      @Nullable public List<String> getDartEntrypointArgs()
      The Dart entrypoint arguments will be passed as a list of string to Dart's entrypoint function.

      A value of null means do not pass any arguments to Dart's entrypoint function.

      Subclasses may override this method to directly control the Dart entrypoint arguments.

    • getDartEntrypointLibraryUri

      @Nullable public String getDartEntrypointLibraryUri()
      The Dart library URI for the entrypoint that will be executed as soon as the Dart snapshot is loaded.

      Example value: "package:foo/bar.dart"

      This preference can be controlled by setting a <meta-data> called FlutterActivityLaunchConfigs.DART_ENTRYPOINT_URI_META_DATA_KEY within the Android manifest definition for this FlutterFragmentActivity.

      A value of null means use the default root library.

      Subclasses may override this method to directly control the Dart entrypoint uri.

    • getInitialRoute

      protected String getInitialRoute()
      The initial route that a Flutter app will render upon loading and executing its Dart code.

      This preference can be controlled with 2 methods:

      1. Pass a boolean as FlutterActivityLaunchConfigs.EXTRA_INITIAL_ROUTE with the launching Intent, or
      2. Set a <meta-data> called FlutterActivityLaunchConfigs.INITIAL_ROUTE_META_DATA_KEY for this Activity in the Android manifest.
      If both preferences are set, the Intent preference takes priority.

      The reason that a <meta-data> preference is supported is because this Activity might be the very first Activity launched, which means the developer won't have control over the incoming Intent.

      Subclasses may override this method to directly control the initial route.

      If this method returns null and the shouldHandleDeeplinking returns true, the initial route is derived from the Intent through the Intent.getData() instead.

    • getCachedEngineId

      @Nullable protected String getCachedEngineId()
      Returns the ID of a statically cached FlutterEngine to use within this FlutterFragmentActivity, or null if this FlutterFragmentActivity does not want to use a cached FlutterEngine.
    • getCachedEngineGroupId

      @Nullable protected String getCachedEngineGroupId()
    • getBackgroundMode

      @NonNull protected FlutterActivityLaunchConfigs.BackgroundMode getBackgroundMode()
      The desired window background mode of this Activity, which defaults to FlutterActivityLaunchConfigs.BackgroundMode.opaque.
    • getRenderMode

      @NonNull protected RenderMode getRenderMode()
      Returns the desired RenderMode for the FlutterView displayed in this FlutterFragmentActivity.

      That is, RenderMode.surface if getBackgroundMode() is FlutterActivityLaunchConfigs.BackgroundMode.opaque or RenderMode.texture otherwise.

    • provideRootLayout

      @NonNull protected FrameLayout provideRootLayout(Context context)
      Returns a FrameLayout that is used as the content view of this activity.