How to detect when app is minized in flutter

Viewed 1073

Is there a way to detect when the app has been minimized? Simply using WidgetsBindingObserver with the paused event doesn't work as it's indistinguishable from when the user turns off the screen / phone locks. Note, I need this to work for both android and ios.

A bit of context of what I'm doing. In the application, I'm running a timer. I want to stop this timer if the user minimizes the app (e.g. uses its phone for something else). If the user, however, turns off the screen/locks it, I want the timer to continue.

1 Answers

I suggest to take a look at this package: is_lock_screen

As the description suggest

Useful for determining whether app entered background due to locking screen or leaving app.

I would try with this:

@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
  super.didChangeAppLifecycleState(state);
  if (state == AppLifecycleState.inactive) {
    final isLock = await isLockScreen();
    if(!isLock){
       print('app inactive MINIMIZED!');
    }
    print('app inactive in lock screen!');
  } else if (state == AppLifecycleState.resumed) {
    print('app resumed');
  }
}
Related