I have seen the official update log small partners are aware of the new version flutter
and Android
the native interaction there are some changes, mainly in the following sections
1. Custom Plugin class need to implement FlutterPlugin
the interface, and implement the following two methods
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { //注册,等同于原来的registerWith
final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_wx");
channel.setMethodCallHandler(new FlutterWxPlugin());
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
}
2. Get Flutter
data (method name) needed to achieve the passed MethodCallHandler
(and previous versions of the same)
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
Log.d(TAG, "onMethodCall:" + activity);
Toast.makeText(activity, "TestActivity", Toast.LENGTH_SHORT).show();
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
3. Now there is a demand that is needed onMethodCall
to obtain activity object methods, the new version provides us with a new interface to make me a better deal Plugin
and Activity
some interactive, so we implement ActivityAware
this interface, the following method overrides, in onAttachedToActivity
the we can binding.getActivity()
get the current activity
object, and then assign the global variables we define, this time the question is, we onMethodCall
get in the way activity
of value is alwaysnull
@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
Log.d(TAG, "onAttachedToActivity");
this.activity = binding.getActivity();
}
@Override
public void onDetachedFromActivityForConfigChanges() {
}
@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
}
@Override
public void onDetachedFromActivity() {
}
4. Solution, modified onAttachedToEngine
method channel.setMethodCallHandler(new FlutterWxPlugin())
is channel.setMethodCallHandler(this)
;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { //注册,等同于原来的registerWith
final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_wx");
channel.setMethodCallHandler(this);//修改此处为this
}
5. Summary, the specific cause of the problem has not been studied fine, when the Plugin
method of replication will be performed twice at initialization time. We look forward to the official solution.
flutter as the savior of cross-platform, many of my friends want to start but do not know where to start, here to share a good learning from entry to advanced study and organize video files.
Want to learn video friends can click I learn more about oh
A total of 19 PDF documents for learning, a friend in need private letter I [] flutter or to receive a free Comments
01 set up a development environment on Windows Flutter
02 Configuration Editor
03 Flutter Flutter create a new application from our template
04 write your first Flutter App
05 Flutter in building layout
06 Flutter Widget Framework
07 is the basis of shared theme colors and font styles
08 display pictures online
09 List collection
10 List handle clicks and gestures
AppBar 11 Example Procedure
Tabbed sample program of 12 AppBar
13 Flutter learn how to compile Android project
14 Flutter of resource loading
15 Using Flutter IDE
16 Flutter of a thermal overload
17 Flutter of debugging Flutter App
18 Flutter of release for Android APP
Published 19 Flutter version of IOS APP