颤振:检查应用生命周期事件挂起



在关于AppLifecycle Event的Flutter文档中,它有4个事件。

  • 无效
  • 暂停
  • 恢复
  • 暂停

在上面的 4 个事件中,我可以打印 AppLifecycle 事件处于非活动状态、已暂停、已恢复,但我无法处理挂起事件。 因为我想检查用户是否从任务管理器杀死/破坏应用程序。如果用户杀死/破坏应用程序,我想显示安全代码(如密码等(。 如何处理暂停事件?

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
AppLifecycleState _appLifecycleState;
FunctionHelper functionHelper = FunctionHelper();
PageController _pageController;
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
_pageController = PageController(initialPage: 0);
super.initState();
}
@override
void didChangeAppLifecycleState(AppLifecycleState appLifecycleState) {
setState(() {
_appLifecycleState = appLifecycleState;
});
print(appLifecycleState);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
if (_appLifecycleState == null) {
return Center(
child: Text('This widget has not observed any lifecycle changes.'),
);
} else {
return Center(
child: Text(
'The most recent lifecycle state this widget observed was: $_appLifecycleState'),
);
}
}

Flutter 没有任何 API 可以使用 Activity Flutter/Flutter#21982onDestroy方法。在挂起事件期间,应用程序会暂时挂起,这相当于在 Android 中onStop。当用户杀死或破坏应用程序时,onDestroy会在Android中触发,因此无法使用挂起事件进行处理。

最新更新