8

I am actually aware of this implementation: Flutter programmatically closing the app, but when the app closes is still in background. How can I absolutely kill the app and not let it be in background?

Thank you!

Lu Chinke
  • 622
  • 2
  • 8
  • 27
  • Did you try `exit(0);` in Android? In iOS there is no such thing as exiting an app. – Günter Zöchbauer Jul 19 '18 at 14:05
  • @GünterZöchbauer Yes. In the description i told you i am aware of this, but doesn't work as i wish. – Lu Chinke Jul 19 '18 at 14:09
  • Possible duplicate of [Flutter how to programmatically exit the app](https://stackoverflow.com/questions/45109557/flutter-how-to-programmatically-exit-the-app) –  Aug 17 '19 at 10:21

3 Answers3

6

If you don't want to use invokeMethod, you can directly close the app using

SystemNavigator.pop(); // Only works on Android.

While exit(0) works on both iOS and Android, it shouldn't be used on either. Read this answer for more information.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • How do I exit and kill all the threads/processes when user manually kills the app? In my flutter app(on Android) some processes which were meant to collect sensor data still run even after I close the app. It only stops when I go on app info and do "FORCE STOP". – Gauranga Sep 09 '19 at 11:28
  • @Gauranga You can use `exit(0)` for that. – CopsOnRoad Sep 09 '19 at 15:27
  • where do I call exit (0) . The onDespose methoes of UI are not called when user kill the app. I am wondering where should I call exit(0) . I want to release all the resources on exit call. – Gauranga Sep 10 '19 at 07:50
  • @Gauranga Can you post it as a question, I'd be happy to answer it. – CopsOnRoad Sep 10 '19 at 08:00
  • I hv asked it here https://stackoverflow.com/questions/57854764/how-to-force-stop-flutter-app-when-user-kills-the-app – Gauranga Sep 11 '19 at 09:44
5

You can use exit(0). But app exits abruptly. You can use the below code which is much smoother.

SystemChannels.platform.invokeMethod<void>('SystemNavigator.pop')

Documentation is available here.

MrDumb
  • 2,140
  • 1
  • 21
  • 31
  • thanks for the answer. Need to check it later, but if any user agrees with you, i mark your answer as the correct one! – Lu Chinke May 30 '19 at 12:38
0

SystemNavigator.pop() will work for Android but it won't work in Ios side.

and

exit(0) will work for both systems but is not recommended because Ios may reject your app due to their guidelines and in Android, it stops the Dart VM process

Hardik Hirpara
  • 2,594
  • 20
  • 34