List the responsibilities of FlutterActivity
#android#native#flutteractivity
Answer
Overview
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 textFlutterActivity
text
FlutterActivityKey Responsibilities of FlutterActivity
1. Host the Flutter Engine
text
FlutterActivitytext
FlutterEnginekotlin// Default — Flutter creates and manages the engine class MainActivity : FlutterActivity()
2. Render Flutter UI via FlutterView
It attaches a
text
FlutterView3. Lifecycle Management
text
FlutterActivitykotlin// 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
BinaryMessengerkotlinclass 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
FlutterActivitytext
GeneratedPluginRegistrantkotlin// This is called automatically by FlutterActivity GeneratedPluginRegistrant.registerWith(flutterEngine)
6. Handle Intent and Deep Links
kotlinclass 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
| Feature | FlutterActivity | FlutterFragment |
|---|---|---|
| Type | Android Activity | Android Fragment |
| Use case | Full-screen Flutter app | Flutter embedded in native screen |
| Flexibility | Less — owns the whole screen | More — part of a native layout |
| Add-to-app | ❌ Not ideal | ✅ Recommended for add-to-app |
Customizing FlutterActivity
kotlinclass 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
| Responsibility | Detail |
|---|---|
| Host Flutter engine | Creates/manages text |
| Render UI | Attaches text |
| Lifecycle forwarding | text text text |
| Platform channels | Sets up text |
| Plugin registration | Calls text |
| Deep link handling | text |
| Back press handling | Routes text |
| Transparency/background | Configures text text |