MainActivityのonBackPressedをオーバーライドしたら対応できました。
class MainActivity: FlutterFragmentActivity() {
private var backDisabled = false
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
val mychannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "com.xxx/channel")
mychannel.setMethodCallHandler {
call, result ->
if (call.method == "setBackDisabled") {
backDisabled = call.arguments as? Boolean ?: false
}
}
}
override fun onBackPressed() {
if (!backDisabled) {
super.onBackPressed()
}
}
}
後はFlutter側でよんであげるだけ
static const MethodChannel _channel =
MethodChannel('com.xxx/channel');
static Future<void> setBackDisabled(bool disabled) async {
await _channel.invokeMethod('setBackDisabled', disabled);
}