Reputation: 7640
I have a simple Android app. I imported a new flutter module. When I pass to the Flutter page, I can not implement native code anymore. I got an error No implementation method found for method getBatteryLevel
. How can I fix this?
Note: If I run the flutter module itself, I can get the result of native code.
The triggered method in main.dart:
String result = await methodChannel.invokeMethod('getBatteryLevel');
MainActivity of Flutter module:
package com.deremakif.flutter_counter_module.host;
import io.flutter.embedding.android.FlutterActivity;
import androidx.annotation.NonNull;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "samples.flutter.io/battery";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(((methodCall, result) -> {
if (methodCall.method.equals("getBatteryLevel")) {
result.success("batteryLevel"); // It returns string "batteryLevel".
} else {
result.notImplemented();
}
}));
}
}
Login button to change the page from Android to Flutter:
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(
FlutterActivity.createDefaultIntent(getApplicationContext())
);
}
});
You can find the whole project to reproduce the error here.
Upvotes: 1
Views: 1738
Reputation: 1544
When you use your flutter project as a library module in another project, and navigate to the Flutter part using FlutterActivity.createDefaultIntent(getApplicationContext())
, your MainActivity will not be evaluated. I suggest that you create a new Activity in your native app (LoginApp in your case) and have it extending FlutterActivity, and start it when you want to navigate to the flutter part (see below)
// in LoginApp
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
class MyFlutterActivity extends FlutterActivity {
private static final String CHANNEL = "samples.flutter.io/battery";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(((methodCall, result) -> {
if (methodCall.method.equals("getBatteryLevel")) {
result.success("batteryLevel"); // It returns string "batteryLevel".
} else {
result.notImplemented();
}
}));
}
}
and then start it like this
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(
new FlutterActivity.NewEngineIntentBuilder(MyFlutterActivity.class).build(getApplicationContext())
);
}
});
And don't forget to list this activity in your android manifest (in LoginApp)
<activity android:name=".ui.login.MyFlutterActivity"
android:theme="@style/AppTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
/>
Upvotes: 4
Reputation: 1619
Please specify the method channel in your dart code as exact as the CHANNEL string in your native code. For this scenario, you will have to write the below code in your flutter side code
static const methodChannel = const MethodChannel('samples.flutter.io/battery');
String result = await methodChannel.invokeMethod('getBatteryLevel');
Upvotes: 1