1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Flutterで一時的にAndroidの戻る操作を無効にする方法

Posted at

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);
  }
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?