1

How to release foreground to the previous application in Flutter (Dart) ? I mean, how to force to call the onPause or viewWillDisappear to let my app disappear and let the previous app come back to the foreground.

Is there a method thant I can call ?

Edit : I don't wan't to close my app, juste "minimize" it.

Jérémy
  • 1,790
  • 1
  • 24
  • 40
  • Possible duplicate of [Flutter programmatically closing the app](https://stackoverflow.com/questions/45109557/flutter-programmatically-closing-the-app) – Karan Harsh Wardhan Jan 11 '19 at 09:04
  • I don't want to close my app. I juste want to "minimize" it. I mean, let the previous app to come back in the foreground without closing mine. – Jérémy Jan 12 '19 at 10:10

2 Answers2

2

You are struggling with a mismatch between Flutter's architecture and Android's. In your previous question you needed a way to bring your flutter app to the foreground, to which the answer is a full-screen intent notification. The problem is that in native Android, you would probably have used the NEW_TASK flag to start a new task. As Flutter only has one activity, it's necessary to use USE_CURRENT instead.

With NEW_TASK, you would use Activity.finish() to close it, closing just the new activity. If you did that with Flutter, that would probably close the whole app (because of the use of USE_CURRENT).

It might be possible to have a native Android app (allowing you to have more direct control of the launch of activities) and to use Add2App to add the Flutter screen(s). If you get that to work, I'd like to know.

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • Thank you again for your precise answer. But as it said in GitHub : Add2App "_APIs and tooling are not stable_" and it make me uncomfortable to take a way in a not reliable technology. Is there another cross-platform solution with flutter to properly "minimize" my app without closing it ? I want to avoid as much as possible to duplicate methods in both plateforms. I know even less about IOS than on Android. But it seems that there is an event of releasing foreground in both plateforms. I'm pretty sur that we can force it. **Your help is really appreciate Richard** – Jérémy Jan 12 '19 at 10:53
  • 1
    You should read the whole section on activities particularly the bit on [tasks](https://developer.android.com/guide/components/activities/tasks-and-back-stack). To pop your activity you need to call finish, but if that's your *only* activity then you exit - that's the fundamental conflict between Flutter and `NEW_TASK`. I believe that the solution will lie somewhere in the Add2App area, but I haven't found it yet. (I need it too ;-) ) Check out other frameworks like React Native which may behave differently and be better for your use case. – Richard Heap Jan 12 '19 at 12:13
2

I finally got a solution ! I haven't found yet a solution for the IOS side : I'm working on it.

I used MethodChannel to ask to the native side to minimize itself. For Android use this.moveTaskToBack(true); ! If you got an Objectif-C alternative, it will be perfect !

Dart:

class _MyHomePageState extends State<MyHomePage> {
 static const MethodChannel actionChannel = MethodChannel('method.channel');

  Future<void> _minimize() async{
   try{
     await actionChannel.invokeMethod('minimize');
   } on PlatformException catch(e) {
     print('${e.message}');
   }
 }
}

Android:

public class MainActivity extends FlutterActivity {

 private static final String ACTION_CHANNEL = "method.channel";

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // Action-post-alert method
    new MethodChannel(getFlutterView(), ACTION_CHANNEL).setMethodCallHandler(
            new MethodCallHandler() {
                @Override
                public void onMethodCall(MethodCall call, Result result) {
                    if (call.method.equals("minimize")) {
                        this.moveTaskToBack(true);
                        result.success(true);
                    } else {
                        result.notImplemented();
                    }
                }
            }
    );
 }
}
Jérémy
  • 1,790
  • 1
  • 24
  • 40