Integrate a Flutter module into your Android project
Posted April 22, 2021 by Istiq Septiana ‐ 2 min read
Flutter can handle incoming intents from Android by directly talking to the Android layer and requesting the data that was shared. The basic flow implies that we first handle the shared text data on the Android native side (in our Activity), and then wait until Flutter requests for the data to provide it using a MethodChannel.
HOW TO USE IT (In Android)
Step 1: Add FlutterActivity to AndroidManifest.xml
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
/>
Step 2: Setup Flutter Engine
private fun setupFlutterEngine() {
flutterEngine = FlutterEngine(this)
flutterEngine.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
FlutterEngineCache.getInstance().put("flutter_engine", flutterEngine)
}
Step 3: Setup MethodChannel
private fun setupMethodChannel() {
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger, "com.innocent.audioresearch/audio"
).setMethodCallHandler { call, result ->
when (call.method) {
"getTitle" -> {
result.success("title" + call.argument<String>("audioTitle"))
}
}
}
}
Step 4: Launch Flutter Activity and Send Data
button.setOnClickListener {
setupFlutterEngine()
setupMethodChannel()
startActivity(
FlutterActivity
.withCachedEngine(FLUTTER_ENGINE_ID)
.build(context))
}
HOW TO USE IT (In Flutter)
@override
void initState() {
super.initState();
getAudioFromAndroid();
sendDataTitleToAndroid();
}
String _audioTitle = "No data";
getAudioFromAndroid() async {
var title = await platform.invokeMethod('getTitle');
if (title != null) {
setState(() {
_audioTitle = title;
});
}
}
sendDataTitleToAndroid() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await platform.invokeMethod('getTitle', {"audioTitle": "title song"});
}
DEMO
Here’s an example of implementation.