Flutter and Android pass data to each other
(I) Android code Settings
1. Open Android Studio and create an application Dev.android.book 2, create a MyApplication on the Android :name attribute of the application in androidmanifest.xml. 4. Create an instance of MethodChannel, specify a unique string for this instance, such as dev.android.book/add 5, and set the MethodChannel method callback. Android sends messages to the Flutter using the name of the MethodCall object. Also through the invokeMethod method of the methodChannel object
Add instructions for each of the above steps
Open Android Studio =>File=>New Project=>Select a Project Template=>input Name, Package Name, etc. =>Finish
BookApplication is created to pre-initialize FlutterEngine. There are three ways to start Flutter
A, the first way is to create the default FlutterEngine. Such as FlutterActivity createDefaultIntent (this) b, the second way, Is to create new FlutterEngine such as FlutterActivity. WithNewEngine (). The build (this) c, a third way, Is to use the cache FlutterEngine such as FlutterActivity. WithCachedEngine (ENGINE_ID). Build (this)
Simple analysis of the three ways a and b are the same, FlutterActivity. CreateDefaultIntent actually call or FlutterActivity. WithNewEngine () method as shown in the figure below,
If a and B methods are not recommended for development, it is because during the development process, when the Flutter page is opened in the original sound, there will be a black screen for a period of time. This is because FlutterEngine initializes, after initialization, the Flutter page can be displayed
3. There are two ways to create FlutterEngine instances: pass Context directly or pass Context dartVmArgs
Create an instance of MethodChannel, providing two constructors, Messenger and name, message and name, and MethodCodec
5. Set a callback to the MethodChannel method that receives the contents of a Flutter and provides a MethodCallHandler
6. Flutter passes various values through the interface MethodCallHandler
So the abstract method in MethodCallHandler, onMethodCall, onMethodCall has two parameters in it and MethodCall contains some information about the parameters that were passed in like the method name, the parameters,
Result indicates successful or failed callback information
7. The Android side sends messages to the Flutter side by calling the invokeMethod method with two parameters, the method name and the parameter, via the registered MethodChannel
C. It is recommended to use this method of pre-initialization of FlutterEngine. During the process of use, the page almost does not pause to open the Flutter page, so the pre-initialization code in Application is as follows
(2) Flutter code Settings
Create an instance of MethodChannel with the package name my_flutter 2, specify a unique string for this instance, such as dev.android.book/add 3, and set the MethodChannel method callback. Receives messages from the Flutter via the MethodChannel. 4. Android sends a lot of messages to the Flutter based on the method name in the MethodCall object. Also through the invokeMethod method of the methodChannel object
Add instructions for each of the above steps
Create a flutter model and add it as a dependency to the original code by running the flutter create -t module –org com.flutter my_flutter command
Initialize MethodChannel directly in the class, such as final _channel = MethodChannel(channel_name); The argument is a unique string that must be the same as the string on the Android side
3. Set a callback to MethodChannel with the same parameters as on Android
4. Messages sent from Android are also judged by MethodCall, and can be judged by method name
5. The Flutter side sends messages to the Android side by calling invokeMethod via the registered MethodChannel
(3) Introduce the FLutter project just created into the Android project
Add the following code to settings.gradle in the Android project
Then add the Flutter engineering module to the dependencies
This completes the process of adding a Flutter module to an existing Android project and how Android and FLUTTER data are transferred.