A Flutter module that provides a text input screen and returns the entered value to the host Android app using startActivityForResult. The platform code is embedded in the AAR, so no additional code is required in the host app.
- Simple text input interface
- Returns data to host app using Android Activity result mechanism
- Self-contained AAR with all platform code included
- No configuration required in host app
To build the AAR files:
flutter build aarThis will generate AAR files for debug, profile, and release builds in build/host/outputs/repo.
In your host app's app/build.gradle, add the repositories:
repositories {
maven {
url '/Users/calvinmuller/Sites/flutter_module_test/build/host/outputs/repo'
}
maven {
url 'https://storage.googleapis.com/download.flutter.io'
}
}Add the Flutter module dependencies:
dependencies {
debugImplementation 'com.example.flutter_module_test:flutter_debug:1.0'
profileImplementation 'com.example.flutter_module_test:flutter_profile:1.0'
releaseImplementation 'com.example.flutter_module_test:flutter_release:1.0'
}In your app's build.gradle, add the profile build type if it doesn't exist:
android {
buildTypes {
profile {
initWith debug
}
}
}In your Android Activity, use startActivityForResult to launch the Flutter input screen:
import android.content.Intent;
import com.example.flutter_input_plugin.FlutterInputActivity;
public class MainActivity extends AppCompatActivity {
private static final int FLUTTER_INPUT_REQUEST = 1001;
private void launchFlutterInput() {
Intent intent = new Intent(this, FlutterInputActivity.class);
startActivityForResult(intent, FLUTTER_INPUT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FLUTTER_INPUT_REQUEST && resultCode == RESULT_OK) {
String resultData = data.getStringExtra("result_data");
// Use the returned data
Log.d("MainActivity", "Received data: " + resultData);
}
}
}For Kotlin:
import android.content.Intent
import com.example.flutter_input_plugin.FlutterInputActivity
class MainActivity : AppCompatActivity() {
companion object {
private const val FLUTTER_INPUT_REQUEST = 1001
}
private fun launchFlutterInput() {
val intent = Intent(this, FlutterInputActivity::class.java)
startActivityForResult(intent, FLUTTER_INPUT_REQUEST)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == FLUTTER_INPUT_REQUEST && resultCode == RESULT_OK) {
val resultData = data?.getStringExtra("result_data")
// Use the returned data
Log.d("MainActivity", "Received data: $resultData")
}
}
}- The host app launches
FlutterInputActivityusingstartActivityForResult - The Flutter module displays a text input screen
- User enters text and clicks Submit
- The Flutter code communicates with the native Android code via MethodChannel
- The Activity sets the result with the entered text and finishes
- The host app receives the data in
onActivityResult
This module uses a plugin-based architecture for better code organization and proper AAR packaging:
- lib/main.dart: Flutter UI with text input and submit button
- plugins/flutter_input_plugin/: Flutter plugin containing all platform-specific code
- android/src/main/kotlin/.../FlutterInputActivity.kt: Custom FlutterActivity that handles result passing
- android/src/main/AndroidManifest.xml: Declares the Activity (automatically included in AAR)
- The plugin is included as a path dependency and automatically bundled with the module AAR
The text entered by the user is returned with the key "result_data" in the result Intent.
- Flutter SDK
- Android API level 21 or higher
- Java 8 or higher / Kotlin support
- The AAR includes all necessary platform code, so the host app only needs to launch the Activity
- The Activity is properly configured with
android:exported="true"to allow external apps to launch it - All Flutter engine initialization and MethodChannel setup is handled internally