Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter detect killing off the app

Tags:

flutter

I would like to know whether detect killing off the app is possible or not. Let’s say in a chat app, I was able to get timestamp when user leaves the chat room by using onWillPop. But if user killed off the app directly from the chat room, it won’t be fired off. So is there a way to detect that?
Or any suggestions to get timestamp different way?

like image 861
Daibaku Avatar asked Aug 29 '18 09:08

Daibaku


People also ask

How do you know if an app is killed?

there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.

How do I stop apps running in background Flutter?

Run app in background You must call FlutterBackground. initialize() before calling FlutterBackground. enableBackgroundExecution() . you can stop the background execution of the app.

Why is Exit 0 not preferred for closing an app Flutter?

Close Android App With Code: exit(0) : This command also works but it is not recommended because it terminates the Dart VM process immediately and users may think that the app got crashed.


1 Answers

See also https://flutter.io/flutter-for-android/#how-do-i-listen-to-android-activity-lifecycle-events

You can listen for inactive, paused, and detached. This might be a bit too early but usually it's better to do some cleanup a bit too early and too often than not at all:

WidgetsBinding.instance.addObserver(LifecycleEventHandler(     detachedCallBack: () async => widget.appController.persistState(),     resumeCallBack: () async {       _log.finest('resume...');     })); 
class LifecycleEventHandler extends WidgetsBindingObserver {   LifecycleEventHandler({this.resumeCallBack, this.detachedCallBack});    final FutureVoidCallback resumeCallBack;   final FutureVoidCallback detachedCallBack;  //  @override //  Future<bool> didPopRoute()  //  @override //  void didHaveMemoryPressure()    @override   Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {     switch (state) {       case AppLifecycleState.inactive:       case AppLifecycleState.paused:       case AppLifecycleState.detached:         await detachedCallBack();         break;       case AppLifecycleState.resumed:         await resumeCallBack();         break;     }     _log.finest(''' =============================================================                $state ============================================================= ''');   }  //  @override //  void didChangeLocale(Locale locale)  //  @override //  void didChangeTextScaleFactor()  //  @override //  void didChangeMetrics();  //  @override //  Future<bool> didPushRoute(String route) } 

Edit

With this pull request on 4th November 2019, the enum AppLifecycleState.suspending was renamed to AppLifecycleState.detached. If you are using Flutter with a version prior to 1.12, you must still use AppLifecycleState.suspending.

like image 170
Günter Zöchbauer Avatar answered Sep 22 '22 16:09

Günter Zöchbauer