Home > Net >  Flutter overide method back()
Flutter overide method back()

Time:04-23

How can I make the back button work in android the same way as home. If I click on back, the application is killed, but I need the back button to work the same way as home, that is, just hide the application with the state saved. I know that it is possible to track the pressing of the back button using the WillPopScope widget in the onWillPop method, but how can I make the application collapse?

I found the system_shortcuts package, but it is already outdated and does not support null safety for my project.

Problem with system_shortcuts: i just add in pubspec system_shortcuts: ^1.0.0 and in my dart file import 'package:system_shortcuts/system_shortcuts.dart';

console: Error: Cannot run with sound null safety, because the following dependencies don't support null safety:

  • package:system_shortcuts

For solutions, see https://dart.dev/go/unsound-null-safety 3

FAILURE: Build failed with an exception.

  • Where: Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1102

  • What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'.

Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

CodePudding user response:

You can run the app with system_shortcuts package by the below command

flutter run --no-sound-null-safety

OR if you want to keep your app on null safety then you can use intent package for navigating to home like android home button.

system_shortcuts package's home navigation code is

private void home() {
    this.activity.startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
}

You can call this java code by intent package like below.

Intent()
    ..setAction(Action.ACTION_MAIN)
    ..addCategory("android.intent.category.HOME")
    ..startActivity().catchError((e) => print(e));

OR You can do it by flutter methodChannel

CodePudding user response:

For my purpose I found package move_to_background, and use MoveToBackground.moveTaskToBack();

  • Related