I’ve got a StatefulWidget in my Flutter app. The StatefulWidget has the following property set:
final pausedTimeAllowed = 0;
Here’s my implementation of didChangeAppLifecycleState within the State class for that widget:
@override void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (!(ModalRoute.of(context)!.isCurrent)) {
return;
}
switch (state) {
case AppLifecycleState.resumed:
debugPrint("didChangeAppLifecycleState: resumed");
if ((globals.pausedSince is DateTime) && (DateTime.now().difference(globals.pausedSince).inSeconds > widget.pausedTimeAllowed)) {
globals.pausedSince = null;
Navigator.of(context).pushReplacementNamed('/');
} else {
setState(() {
showInactiveOverlay = false;
});
}
break;
case AppLifecycleState.inactive:
debugPrint("didChangeAppLifecycleState: inactive");
setState(() {
showInactiveOverlay = true;
});
break;
case AppLifecycleState.paused:
debugPrint("didChangeAppLifecycleState: paused");
if (widget.pausedTimeAllowed > 0) {
debugPrint("didChangeAppLifecycleState: paused | setting `pausedSince`…");
globals.pausedSince = DateTime.now();
} else {
debugPrint("didChangeAppLifecycleState: paused | navigating to /…");
Navigator.of(context).pushReplacementNamed('/');
}
break;
}
}
When this route is visible on the user’s screen and they put the phone to sleep, I notice that the app's inactive and paused states are triggered but on unlocking the device on the app, I briefly (for about 0.5—1 seconds) see a flash of the screen before being navigated to /.
Why is it not already on /?
Additionally, despite the app's inactive state being triggered, it's noteworthy that I'm not see the inactive overlay (showInactiveOverlay) that does appear over the content when the app is backgrounded (e.g. when it goes into the switcher).
Am I pulling on the wrong thread by expecting the didChangeAppLifecycleState callback to handle these?