Question #353HardNative Integration

List the responsibilities of FlutterActivity

#android#native#flutteractivity

Answer

Overview

text
FlutterActivity
is the core Android Activity class provided by the Flutter engine that hosts the Flutter UI in an Android app. Every Flutter app on Android runs within a
text
FlutterActivity
(or a subclass of it).


Key Responsibilities of FlutterActivity

1. Host the Flutter Engine

text
FlutterActivity
creates and manages the
text
FlutterEngine
— the runtime that executes Dart code and renders Flutter UI.

kotlin
// Default — Flutter creates and manages the engine
class MainActivity : FlutterActivity()

2. Render Flutter UI via FlutterView

It attaches a

text
FlutterView
(SurfaceView/TextureView) to the Activity, where the Flutter rendering engine draws the UI.

3. Lifecycle Management

text
FlutterActivity
forwards Android lifecycle events to the Flutter engine:

kotlin
// FlutterActivity internally forwards these to Dart/Flutter
override fun onResume() → notifies Flutter engine
override fun onPause() → suspends Flutter
override fun onDestroy() → cleans up FlutterEngine
override fun onBackPressed() → dispatches to Flutter's Navigator

4. Handle Platform Channels

It sets up the

text
BinaryMessenger
for Platform Channel communication between Flutter (Dart) and Android (Kotlin/Java).

kotlin
class MainActivity : FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        // Register platform channel
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "com.example/battery")
            .setMethodCallHandler { call, result ->
                if (call.method == "getBatteryLevel") {
                    result.success(getBatteryLevel())
                } else {
                    result.notImplemented()
                }
            }
    }
}

5. Plugin Registration

text
FlutterActivity
automatically registers all Flutter plugins via
text
GeneratedPluginRegistrant
:

kotlin
// This is called automatically by FlutterActivity
GeneratedPluginRegistrant.registerWith(flutterEngine)

6. Handle Intent and Deep Links

kotlin
class MainActivity : FlutterActivity() {
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        // Handle deep links, push notification taps, etc.
    }
}

7. Configure Transparent Background / No History

kotlin
// Configuring FlutterActivity via Intent
val intent = FlutterActivity
    .withNewEngine()
    .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
    .build(context)
startActivity(intent)

FlutterActivity vs FlutterFragment

FeatureFlutterActivityFlutterFragment
TypeAndroid ActivityAndroid Fragment
Use caseFull-screen Flutter appFlutter embedded in native screen
FlexibilityLess — owns the whole screenMore — part of a native layout
Add-to-app❌ Not ideal✅ Recommended for add-to-app

Customizing FlutterActivity

kotlin
class MainActivity : FlutterActivity() {
    // Change the Dart entrypoint
    override fun getDartEntrypointFunctionName() = "customMain"

    // Use a pre-warmed engine
    override fun provideFlutterEngine(context: Context): FlutterEngine? {
        return MyApp.flutterEngine // Shared pre-warmed engine
    }

    // Custom initial route
    override fun getInitialRoute() = "/onboarding"
}

Summary of Responsibilities

ResponsibilityDetail
Host Flutter engineCreates/manages
text
FlutterEngine
Render UIAttaches
text
FlutterView
to Activity
Lifecycle forwarding
text
onResume
,
text
onPause
,
text
onDestroy
→ Flutter
Platform channelsSets up
text
BinaryMessenger
Plugin registrationCalls
text
GeneratedPluginRegistrant
Deep link handling
text
onNewIntent
forwarding
Back press handlingRoutes
text
onBackPressed
to Flutter Navigator
Transparency/backgroundConfigures
text
FlutterSurfaceView
or
text
FlutterTextureView